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
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言 字符串操作函数
- C语言字符串操作函数
- c语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- c语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- c语言字符串操作函数
- C语言字符串操作函数
- for-in 来遍历字典 while for 循环
- 云服务正在吞噬世界!
- Java编程思想重点笔记(Java开发必看)
- 选择团队成员,最应该看重什么?
- jQuery基础学习(二)
- C语言字符串操作函数
- wireshark解析rtp协议,流媒体中的AMR/H263/H264包的方法
- 操作系统安装方法(简易安装)
- logback日志配置
- 常用設計模式
- [Spring实战系列](1)Eclipse下创建Spring-HelloWorld项目
- 解决jboss-eap-6 (jboss7)JCE cannot authenticate the provider BC 的问题
- 日历
- 如何写模块化的代码?