C语言字符串操作函数

来源:互联网 发布:淘宝如何不显示会员名 编辑:程序博客网 时间:2024/06/07 08:05
/*file name: sting_func_test.c author: zhongjundescription:sting_func_test demodata:20150701time:PM 22:36key(study):string operatenote:所有模块单独测试,没有试过一起测试,可能会memory fault*/#include <string.h>#include <stdio.h>char dst_string[20] = "hard work";char *src_string = "hello";int main(){size_t src_str_len = 0;size_t dst_str_len = 0;//strlen 返回值不包含NULL,此值返回5src_str_len = strlen(src_string);dst_str_len = strlen(dst_string);printf("src_str_len(%d)\n",src_str_len);printf("dst_str_len(%d)\n",dst_str_len);#ifdef no_len_limit{//strcpy 会把NULL也copy过去,输出hello//如果copy,要保证dst_string有足够的空间printf("dst_string(%s)\n",dst_string);strcpy(dst_string,src_string);printf("dst_string(%s)\n",dst_string);//strcat 会从上个字符串的NULL开始catstrcat(dst_string,src_string);printf("dst_string(%s)\n",dst_string);}#endif#ifdef len_limit{//strncpy copy strlen(src_string)不会copy NULL//strncpy copy len > strlen(src_string) copy NULL,多余的会填充NULLprintf("dst_string(%s)\n",dst_string);strncpy(dst_string,src_string,strlen(src_string));//strncpy(dst_string,src_string,strlen(src_string)+1);printf("dst_string(%s)\n",dst_string);//strncat strncmp同样是带 len limit的}#endif#ifdef find_char{//strchr 返回找到第一个位置,//strrchr 返回找到最后一个位置//strpbrk 找一个字符 groupchar *pos = NULL;pos = strchr(dst_string,'r');if(pos != NULL)printf("pos_strchr(%s)\n",pos);pos = strrchr(dst_string,'r');if(pos != NULL)printf("pos_strrchr(%s)\n",pos);pos = strpbrk(dst_string,"abcde");if(pos != NULL)printf("pos_strpbrk(%s)\n",pos);}#endif#ifdef find_string{char *pos = NULL;pos = strstr(dst_string,"rd");if(pos != NULL)printf("pos_strstr(%s)\n",pos);}#endifreturn 0;}

0 0
原创粉丝点击