您的位置:首页 > 编程语言 > C语言/C++

小例子拾掇知识点之c/c++篇第1话--c分割字符串方法

2013-07-11 17:21 423 查看
日常工作中,分割字符串应该是算常用功能了,虽然简单,但也算涉及到一些知识点了,毕竟第一篇嘛,热热身,先来个简单的。不说那么多,上代码

/*
* 系统环境:windows
* 编译: vc++ 6.0
* 运行: split.exe
* ----------------------------------------------------------------------------------------------------
* Copyright (c) 2013 by shaohuifan
* All rights reserved
* ----------------------------------------------------------------------------------------------------
* 根据分隔符分割字符串
*----------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* 计算出字符@delim在@str中的个数
*/
int CountChar(const char *str, char delim) {
int count = 0;
const char *p = str;  //const char *p意思是p is a pointer to const char
while ( *p ) {
if ( *p == delim )
count++;
p++;
}
return count;
}
/*
* 分割字符串,存储到words中,使用完以后,words必须释放
*/
int SplitStr2Words(char ***words, const char *pstr, char delim) {
int word_count = CountChar(pstr, delim) + 1;
*words = (char **) malloc(sizeof(char *) * word_count);
int i = 0;
const char *p = pstr;
while ( 1 ) {
const char *s = p;
p = strchr(p, delim);
int len = ( p ) ? p - s : strlen(s);
(*words)[i] = (char *) malloc(sizeof(char) * (len + 1));
strncpy((*words)[i], s, len);
(*words)[i][len] = 0;
if ( !p ) break;
p++;
i++;
}
return word_count;
}
/*
* 单元测试
*/
int main(void) {
char *p_test_string = "aa,bb,ccflskdfksdf.sdfjlas;fs/dfdfjsd;fkaf/sadfjasdfsldf/asfjlsfjas/fss\f[[]p[po[oipoip[][]!@@#@$#%#$^%$,f,sdfosd,fsof dfsdf/.,.!@#@$#%$^%&*^(&*)";
printf("%s\n", p_test_string);
char **words = NULL;
int word_count = SplitStr2Words(&words, p_test_string, ',');
printf("split string into %d words:\n", word_count);
int i_counter=0;
for ( ; i_counter < word_count; i_counter++) {
printf("word %d: [%s]\n", i_counter, words[i_counter]);
free(words[i_counter]); //释放word指针
}
free(words); //释放words指针
return 0;
}
嗯,看看这里面有什么知识点呢,我抛砖引玉一下,列几个先,各位看官将就一下。

1,const char *p 和char * const cp的区别

2,c中的字符串操作方法

3,二级指针

我们接下来一个一个聊一下:

1,const char *p 和char * const cp的区别

根据著名教材The C++ Programming Language提到的方法,我们应该从右往左念:

const char *p(p is a pointer to const char)

char* const cp(cp is a const pointer to char)

一个是指针指向的地址的内容不能改变,一个是指针本身的内容(指针指向的地址)不能改变。

这里面有一个是要注意的,就是const char *p中p可以指向malloc分配的内存,并且可以通过memcpy和snprintf去修改指向的值,不过会报编译错误。但是p[1]=x这类操作是不可以的。

如果觉得这两个很容易理解,不够过瘾,可以好好念下下面几个:

char ** p1; (pointer to  pointer to    char )

const char **p2;(pointer to    pointer to const char )

char * const * p3;( pointer to const pointer to    char )

const char * const * p4;(pointer to const pointer to const char )

char ** const p5;(const pointer to    pointer to    char )

const char ** const p6;(const pointer to    pointer to const char )

char
* const * const p7;(const pointer to const pointer to    char )

const
char * const * const p8;(const pointer to const pointer to const char)

2,c中操作字符串的方法

好像没什么好说的,随便列一下,当做个笔记。

strtok:字符串分割函数strstr:字符串查找函数strspn:字符查找函数

strrchr:定位字符串中最后出现的指定字符strpbrk:定位字符串中第一个出现的指定字符strncpy:复制字符串

strncat:字符串连接函数strncasecmp:字符串比较函数(忽略大小写)strlen:字符串长度计算函数

strdup:复制字符串strcspn:查找字符串strcpy:复制字符串

strcoll:字符串比较函数(按字符排列次序)strcmp:字符串比较函数(比较字符串)strchr:字符串查找函数(返回首次出现字符的位置)

strcat:连接字符串strcasecmp:字符串比较函数(忽略大小写比较字符串)

rindex:字符串查找函数(返回最后一次出现的位置)index:字符串查找函数(返回首次出现的位置)toupper:字符串转换函数(小写转大写)

tolower:字符串转换函数(大写转小写)toascii:将整数转换成合法的ASCII码字符strtoul:将字符串转换成无符号长整型数

strtol:将字符串转换成长整型数strtod:将字符串转换成浮点数gcvt:将浮点型数转换为字符串(四舍五入)

atol:将字符串转换成长整型数atoi:将字符串转换成整型数atof:将字符串转换成浮点型数

3,多级指针

我们这里的多级指针主要是表示二维数组的二级指针和二维数组地址的三级指针

我们这里用一个words的二维指针用来指向一个指针数组,每个指针又再指向一个字符串



二级指针也很好理解,只是在跟二维数组结合使用的时候有些东西需要注意下:

例如:

int a[4][5]

这里要好好理解&a和&a[0]以及&a[0][0]的区别,那么在把这些值赋给指针类型变量的时候才不会出错,应该说这三个东西,他们存放的内容都是一样的,

都是指向a的第一元素,关键在于他们的类型不一样,&a的类型是int(*)[4][5],&a[0]是int(*)[5],&a[0][0]就是int
*了。哦,好像很简单哦,呵呵。

哎哟,那a指的是什么呢,那当然就是&a[0]啦,因为a可以看成是每个元素都是5个int的一维数组,那么a当然是指第一个元素的地址啦。

那么这个东西跟二级指针又什么联系呢,联系就是,我能不能用int **p=&a 来获得a的地址呢?

答案不行的,因为int **指向的是int,而&a指向的是数组,所以只有int **p=&a[0][0]了。

各位看官,第一篇就先些这些吧,如果里面有什么错误的,希望不吝赐教,有什么意见或建议,请留言,大家共同交流,共同进步。

如果大家对这个小方法有什么优化的意见,那更是欢迎之至。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  分割字符串 c c++