两个辅助指针变量截取字符串
2015-04-18 10:09
253 查看
如题:有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";)
思路:用一个二维数组来存储截取后的字符串,以' ,'作为分隔符,截取完一个字符串后,更新当前的位置。直到' \0 '.
代码:
View Code
思路:用一个二维数组来存储截取后的字符串,以' ,'作为分隔符,截取完一个字符串后,更新当前的位置。直到' \0 '.
代码:
#include <stdlib.h> #include <string.h> #include <stdio.h> int splitString(const char *buf1, char c, char buf2[10][30], int *count) { char *p=NULL, *pTmp = NULL; int tmpcount = 0; //1. p和ptmp初始化 p = buf1; pTmp = buf1; do { //2.检索符合条件的位置p后移形成差值挖字符串 p = strchr(p, c); if (p != NULL) { if (p-pTmp > 0) { strncpy(buf2[tmpcount], pTmp, p-pTmp); buf2[tmpcount][p-pTmp] = '\0'; //把第一行数据变成 C风格字符串 tmpcount ++; //3.重新让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p!='\0'); *count = tmpcount; return 0; } int main(int argc,char *argv) { int ret = 0, i = 0; char *p1 = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,"; char cTem= ','; int nCount; char myArray[10][30]; ret = splitString(p1, cTem, myArray, &nCount); if (ret != 0) { printf("fucn spitString() err: %d \n", ret); return ret; } for (i=0; i<nCount; i++ ) { printf("%s \n", myArray[i]); } printf("Split complete\n"); return ; }
View Code
相关文章推荐
- 两个辅助指针变量挖字符串(4种实现方式)
- C提高_day03_两个辅助指针变量挖字符串(强化4)
- C提高_day03_两个辅助指针变量挖字符串(强化1)
- C提高_day03_两个辅助指针变量挖字符串(强化2)
- 【C语言提高29】两个辅助指针变量挖字符串
- C提高_day03_两个辅助指针变量挖字符串
- C提高_day03_两个辅助指针变量挖字符串(强化3)
- 截取字符串,汉字占两个字节,字母占一个字节
- .net截取两个字符串中间的内容
- 关于截取字符串的两个函数CString::Tokenize()和AfxExtractSubString() .
- js正则截取两个字符串之间的字符串
- 截取字符串,汉字占两个字节,字母占一个字节
- PHP的两个常用函数截取中文字符串无乱码与获取客户端IP地址
- 截取两个String字符串中间相同部分并进行操作
- mysql 截取指定的两个字符串之间的内容
- 截取字符串,汉字占两个字节,字母占一个字节
- JAVA两个字符串截取相同的最长子字符串
- Java截取特定两个标记之间的字符串
- SQL 截取字符串,中文算两个字符的方法
- Javascript截取字符串的两个方法[载]