C语言中string函数详解(收藏)
更新时间:2023-05-17 15:14:01 阅读量: 实用文档 文档下载
C语言中string函数详解
分类: C/C++学习 2011-08-10 22:18 42人阅读 评论(1) 收藏 举报
PS:本文包含了大部分strings函数的说明,并附带举例说明。本来想自己整理一下的,发现已经有前辈整理过了,就转了过来。修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时崩溃。另外自己重写了部分测试程序,使其更能满足自己测试的需要。不当之处,还请海涵。@函数原型: char *strdup(const char *s)
函数功能: 字符串拷贝,目的空间由该函数分配
函数返回: 指向拷贝后的字符串指针
参数说明: src-待拷贝的源字符串
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include <stdio.h>
2. #include <string.h>
3. #include <alloc.h>
4. int main()
5. {
6. char *dup_str, *string="abcde";
7. dup_str=strdup(string);
8. printf("%s", dup_str);
9. free(dup_str);
10. return 0;
11. }
@函数名称: strcpy
函数原型: char* strcpy(char* str1,char* str2);
函数功能: 把str2指向的字符串拷贝到str1中去
函数返回: 返回str1,即指向str1的指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char string[10];
6. char *str1="abcdefghi";
7. strcpy(string,str1);
8. printf("the string is:%s\n",string);
9. return 0;
10. }
@函数名称: strncpy
函数原型: char *strncpy(char *dest, const char *src,intcount)
函数功能: 将字符串src中的count个字符拷贝到字符串dest中去
函数返回: 指向dest的指针
参数说明: dest-目的字符串,src-源字符串,count-拷贝的字符个数
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. char*src = "bbbbbbbbbbbbbbbbbbbb";//20 'b's
6. char dest[50] ="aaaaaaaaaaaaaaaaaaaa";//20 'a's
7.
8. puts(dest);
9. strncpy(dest, src, 10);
10.
11. puts(dest);
12. return0;
13. }
输出:
view plaincopy to clipboardprint?
1. /*******************************************
2. aaaaaaaaaaaaaaaaaaaa
3. bbbbbbbbbbaaaaaaaaaa
4. *******************************************/
@函数名称: strcat
函数原型: char* strcat(char * str1,char * str2);
函数功能: 把字符串str2接到str1后面,str1最后的'\0'被取消
函数返回: str1
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char buffer[80];
6. strcpy(buffer,"Hello ");
7. strcat(buffer,"world");
8. printf("%s\n",buffer);
9. return 0;
10. }
@函数名称: strncat
函数原型: char *strncat(char *dest, const char *src, size_t maxlen)
函数功能: 将字符串src中前maxlen个字符连接到dest中
函数返回:
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include <stdio.h> 2. #include <string.h>
3. char buffer[80];
4. int main()
5. {
6. strcpy(buffer,"Hello ");
7. strncat(buffer,"world",8);
8. printf("%s\n",buffer);
9. strncat(buffer,"*************",4);
10. printf("%s\n",buffer);
11. return 0;
12. }
@函数名称: strcmp
函数原型: int strcmp(char * str1,char * str2);
函数功能: 比较两个字符串str1,str2.
函数返回: str1<str2,返回负数;str1=str2,返回 0;str1>str2,返回正数.
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include <string.h>
2. #include <stdio.h>
3. int main()
4. {
5. char *buf1="aaa", *buf2="bbb",*buf3="ccc";
6. int ptr;
7. ptr=strcmp(buf2, buf1);
8. if(ptr>0)
9. printf("buffer 2 is greater thanbuffer 1\n");
10. else
11. printf("buffer 2 is less thanbuffer 1\n");
12. ptr=strcmp(buf2, buf3);
13. if(ptr>0)
14. printf("buffer 2 is greater thanbuffer 3\n");
15. else
16. printf("buffer 2 is less thanbuffer 3\n");
17. return 0;
18. }
@函数名称: strncmp
函数原型: int strncmp(char *str1,char *str2,int count)
函数功能: 对str1和str2中的前count个字符按字典顺序比较
函数返回: 小于0:str1<str2,等于0:str1=str2,大于0:str1>str2
参数说明: str1,str2-待比较的字符串,count-比较的长度
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include<string.h>
2. #include<stdio.h>
3. int main()
4. {
5. char str1[] ="aabbc";//
6. char str2[] = "abbcd";//
7. //为使测试程序更简练,此处假定了strncmp只返回-1,0,1三个数
8. char res_info[] = {'<','=','>'};
9. int res;
10.
11. //前1个字符比较
12. res = strncmp(str1, str2, 1);
13. printf("1:str1%c str2\n", res_info[res+1]);
14.
15. //前3个字符比较
16. res = strncmp(str1, str2, 3);
17. printf("3:str1%c str2\n", res_info[res+1]);
18. }
输出:
view plaincopy to clipboardprint?
1. /****************************************
2. 1:str1= str2
3. 3:str1< str2
4. *****************************************/
@函数名称: strpbrk
函数原型: char *strpbrk(const char *s1, const char *s2)
函数功能: 得到s1中第一个“同时也出现在s2中”字符的位置指针
函数返回: 位置指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. char *p="Find all vowels";
6.
7. p=strpbrk(p+1,"aeiouAEIOU");
8. while(p)
9. {
10. printf("%s\n",p);
11. p=strpbrk(p+1,"aeiouAEIOU");
12.
13. }
14. return 0;
15. }
view plaincopy to clipboardprint? 1. /**************************************
2. ind all vowels
3. all vowels
4. owels
5. els
6. **************************************/
@函数名称: strcspn
函数原型: int strcspn(const char *s1, const char *s2)
函数功能: 统计s1中从头开始直到第一个“来自s2中的字符”出现的长度
函数返回: 长度
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. printf("%d\n",strcspn("abcbcadef","cba"));
6. printf("%d\n",strcspn("xxxbcadef","cba"));
7. printf("%d\n",strcspn("123456789","cba"));
8. return 0;
9. }
view plaincopy to clipboardprint? 1. /************************
2. 0
3. 3
4. 9
5. ************************/
@函数名称: strspn
函数原型: int strspn(const char *s1, const char *s2)
函数功能: 统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度
函数返回: 位置指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include<stdio.h>
2. #include<string.h>
3. #include<alloc.h>
4. int main()
5. {
6. printf("%d\n",strspn("abcbcadef","cba"));
7. printf("%d\n",strspn("xxxbcadef","cba"));
8. printf("%d\n",strspn("123456789","cba"));
9. return 0;
10. }
view plaincopy to clipboardprint? 1. /************************
2. 6
3. 0
4. 0
5. ************************/
@函数名称: strchr
函数原型: char* strchr(char* str,char ch);
函数功能: 找出str指向的字符串中第一次出现字符ch的位置
函数返回: 返回指向该位置的指针,如找不到,则返回空指针
参数说明: str-待搜索的字符串,ch-查找的字符
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include<string.h>
2. #include<stdio.h>
3. int main()
4. {
5. char *str = "This is a string!";
6. char ch;
7. char *p;
8.
9. while(1)
10. {
11. printf("Please input a char:");
12. ch = getchar();
13. p = strchr(str, ch);
14. if(p)
15. printf("%c is the %d character of\"%s\"\n",ch, (int)(p-str+1),str);
16. else
17. printf("Not found!\n");
18.
19. printf("Press ESC to quit!\n\n");
20. if(27 == getch())
21. break;
22. fflush(stdin);
23. }
24.
25. return 0;
26. }
运行结果:
view plaincopy to clipboardprint?
1. /********************************************
2. Please input achar:i
3. i is the 3character of "This is a string!"
4. Press ESC to quit!
5.
6. Please input achar:l
7. Not found!
8. Press ESC to quit!
9.
10. Please input achar:s
11. s is the 4character of "This is a string!"
12. Press ESC to quit!
13. **********************************************/
@函数名称: strrchr
函数原型: char *strrchr(const char *s, int c)
函数功能: 得到字符串s中最后一个含有c字符的位置指针
函数返回: 位置指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include<string.h>
2. #include<stdio.h>
3. int main()
4. { 5. charstring[15];
6. char*ptr,c='r';
7. strcpy(string,"This is a string");
8. ptr=strrchr(string,c);
9. if (ptr)
10. printf("The character %c is at position:%d",c,ptr-string);
11. else
12. printf("The character was not found");
13. return 0;
14. }
@函数名称: strstr
函数原型: char* strstr(char* str1,char* str2);
函数功能: 找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
函数返回: 返回该位置的指针,如找不到,返回空指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. char*str1="Open Watcom C/C++",*str2="Watcom",*ptr;
6. ptr=strstr(str1,str2);
7. printf("The substring is:%s\n",ptr);
8. return 0;
9. } 输出:
The substringis:Watcom C/C++
@函数名称: strrev
函数原型: char *strrev(char *s)
函数功能: 将字符串中的所有字符颠倒次序排列
函数返回: 指向s的指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include<string.h>
2. #include<stdio.h>
3. int main()
4. {
5. char forward[]="string"; //原文中定义为char*是不对的,指向代码段的指针
内容是不可变的
6. printf("Before strrev():%s",forward);
7. strrev(forward);
8. printf("Afterstrrev(): %s",forward);
9. return 0;
10. }
输出:
view plaincopy to clipboardprint?
1. /************************************
2. Beforestrrev():string
3. After strrev():gnirts
4. ************************************/
@函数名称: strnset
函数原型: char *strnset(char *s, int ch, size_t n)
函数功能: 将字符串s中前n个字符设置为ch的值
函数返回: 指向s的指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";
6. char letter='x';
7. printf("string before strnset:%s\n",string);
8. strnset(string,letter,10);
9. printf("string after strnset: %s\n",string);
10. 11. return 0;
12. }
输出:
view plaincopy to clipboardprint?
1. /*************************************************
2. string beforestrnset: aaaaaaaaaaaaaaaaaaaaaaa
3. string afterstrnset: xxxxxxxxxxaaaaaaaaaaaaa
4. *************************************************/
@函数名称: strset
函数原型: char *strset(char *s, int ch)
函数功能: 将字符串s中所有字符设置为ch的值
函数返回: 指向s的指针
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. charstring[10]="123456789";
6. charsymbol='c';
7. printf("Before strset(): %s", string);
8. strset(string, symbol);
9. printf("After strset(): %s", string);
10. return 0;
11. }
@函数名称: strtok
函数原型: char *strtok(char *s1, const char *s2)
函数功能: 分解s1字符串为用特定分隔符分隔的多个字符串(一般用于将英文句分解为单词)
函数返回: 字符串s1中首次出现s2中的字符前的子字符串指针
参数说明: s2一般设置为s1中的分隔字符
规定进行子调用时(即分割s1的第二、三及后续子串)第一参数必须是NULL 在每一次匹配成功后,将s1中分割出的子串位置替换为NULL(摘下链中第一个环),因此s1被破坏了
函数会记忆指针位置以供下一次调用
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include<string.h>
2. #include<stdio.h>
3. int main()
4. {
5. char *p;
6. char*buffer;
7. char*delims={ " .," };
8. buffer=strdup("Find words, all of them.");
9. printf("%s\n",buffer);
10. p=strtok(buffer,delims);
11. while(p!=NULL){
12. printf("word: %s\n",p);
13. p=strtok(NULL,delims);
14. }
15. printf("%s\n",buffer);
16. return 0;
17. }//根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字
符串的分隔
PS:根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔
@函数名称: strupr
函数原型: char *strupr(char *s)
函数功能: 将字符串s中的字符变为大写
函数返回:
参数说明:
所属文件: <string.h>
view plaincopy to clipboardprint?
1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char string[]="abcdefghijklmnopqrstuvwxyz",*ptr; //会影响原字符串的内存,
用char[]来声明
6. ptr=strupr(string);
7. printf("%s",ptr);
8. return 0;
9. } @函数名称: strlwr
函数原型: char *strlwr(char *s)
函数功能: 将字符串中的字符变为小写字符
函数返回: 指向s的指针
参数说明:
所属文件: <string.h> view plaincopy to clipboardprint?
1. #include<string.h>
2. int main()
3. {
4. char str[]="HOW TO SAY?";
5. printf("%s",strlwr(str));
6. return 0;
7. }
@函数名称: strerror
函数原型: char *strerror(int errnum)
函数功能: 得到错误信息的内容信息
函数返回: 错误提示信息字符串指针
参数说明: errnum-错误编号
所属文件: <string.h>
view plaincopy to clipboardprint? 1. #include <stdio.h>
2. #include <errno.h>
3. int main()
4. {
5. char *buffer;
6. buffer=strerror(errno);
7. printf("Error: %s",buffer);
8. return 0;
9. } @函数名称: memcpy
函数原型: void *memcpy(void *dest, const void *src, size_t n)
函数功能: 字符串拷贝
函数返回: 指向dest的指针
参数说明: src-源字符串,n-拷贝的最大长度
所属文件: <string.h>,<mem.h>
view plaincopy to clipboardprint? 1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char src[]="******************************";
6. char dest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
7. char *ptr;
8. printf("destination before memcpy:%s\n",dest);
9. ptr=memcpy(dest,src,strlen(src));
10. if (ptr)
11. printf("destination after memcpy:%s\n",dest);
12. else
13. printf("memcpy failed");
14. return 0;
15. }
输出:
view plaincopy to clipboardprint?
1. /*************************************************************
2. destination before memcpy:abcdefghijlkmnopqrstuvwxyz0123456709
3. destination after memcpy:******************************456709
4. **************************************************************/
@函数名称: memccpy
函数原型: void *memccpy(void *dest, const void *src, int c, size_t n)
函数功能: 字符串拷贝,到指定长度或遇到指定字符时停止拷贝
函数返回:
参数说明: src-源字符串指针,c-中止拷贝检查字符,n-长度,dest-拷贝底目的字符串指针 所属文件: <string.h>,<mem.h>
view plaincopy to clipboardprint?
1. #include <string.h>
2. #include <stdio.h>
3. int main()
4. {
5. char *src="This is the source string";
6. char dest[50];
7. char *ptr;
8. ptr=memccpy(dest,src,'c',strlen(src));
9. if (ptr)
10. {
11. *ptr='\0';
12. printf("The character wasfound:%s",dest);
13. }
14. else
15. printf("The character wasn'tfound");
16. return 0;
17. }
输出:
view plaincopy to clipboardprint?
1. /*****************************************
2. The character was found:This is the sourc
3. *****************************************/
PS:指定字符被复制到dest中,memccpy返回了dest中指定字符的下一处的地址,返回NULL表示未遇到指定字符
@函数名称: memchr
函数原型: void *memchr(const void *s, int c, size_t n)
函数功能: 在字符串中第开始n个字符中寻找某个字符c的位置
函数返回: 返回c的位置指针,返回NULL时表示未找到
参数说明: s-要搜索的字符串,c-要寻找的字符,n-指定长度
所属文件: <string.h>,<mem.h>
正在阅读:
C语言中string函数详解(收藏)05-17
电力系统继电保护 第1-3章习题解答06-12
江苏省正地厅级干部简历04-24
采暖系统管道施工工艺01-19
学校管理制度汇编01-07
2021年干部推荐考察组组长讲话提纲08-27
梅兰竹菊图片02-09
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 言中
- 详解
- 函数
- 收藏
- string
- 禅意生活-幸福人生的智慧心法6盘DVD
- ch08索引和查询优化
- 基于波特_五力模型_的商业银行业分析
- 持续推进综合理财服务创新 积极探索证券公司业务转型
- 装饰壁纸施工工艺那天藏0115
- 新核心大学英语2 Unit 4
- 铝压铸件检验及气孔标准
- (最新经营)汽车构造习题集17章已做
- 职场情绪合理表达的三种方法
- 番禺区汉景山庄北侧道路工程勘察设计评标报告
- 《生死牛玉儒》观后感
- 大跨度钢管拱桥缆索吊拼法施工
- 2.食品添加剂使用存在的问题
- 校园电子商务的分析与研究
- 如何计算中央经线
- 人教版语文五年级上册配套练习册参考答案
- 新版ABC股份有限公司采购流程数据图
- 基于GIS的洪水灾害风险区划研究
- 糖尿病健康管理初步方案
- 会计从业资格证财经法规与必背多选题100道