辅助指针变量挖字符串的模式:
1、定义两个指针变量,指向字符串开始位置;
2、让一个字符串移动,两个字符串形成差值;
3、在两个字符串差值之间找需要的字符串;
4、再次让两个字符串对其,准备下一次检索;
代码1、在主函数里创建二维字符数组:
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//
//有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";)
int splitString(const char *buf1, char c, char buf2[10][30], int *count)
{
// strcpy(buf2[0], "aaaaa");
// strcpy(buf2[1], "bbbbb");
char *p=NULL, *pTmp=NULL;
int tmpcount=0;
// 第一步,p和ptmp指针初始化;
do
{
//第二步,检索符合条件的位置,p后移形成差值,挖字符串
p=strchr(p, c);
if(p != NULL)
{
if(p-pTmp >0)
{
strncpy(buf2[tmpcount], p, p-pTmp);
buf2[tmpcount][p-pTmp] = '\0'; //把第一行数据变成C风格的字符串;
tmpcount ++;
//第三步, 让p和pTmp达到下一次检索的条件
pTmp=p=p+1;
}
}else
{
break;
}
}while(*p !='\0');
*count=tmpcount;
return 0;
} //splitString end
int main()
{
int ret=0, i=0;
char *p1="abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
char cTem=',';
int nCount=0;
char myArray[10][30];
ret=splitString(p1, cTem, myArray, &nCount);
if(ret!=0)
{
printf("func splitString() err: %d \n", ret);
return ret;
}
for(i=0;i<nCount; i++)
{
printf("%s \n", myArray[i]);
}
return 0;
}
