常见的c字符串处理函数的源代码以及简单例子【1】
来源:互联网 发布:apache 2.2.15 漏洞 编辑:程序博客网 时间:2024/05/17 08:12
1、strcpy
strcpy 函数 :
char *strcpy(char *strDes, const char *strSrc){ char *address = strDes; assert(((strDes != NULL) && (strSrc != NULL))); while((*strDes++ = *strSrc++) != '\0') ; return address;}
strcpy 函数在vs2012测试例子:
#include<stdio.h>#include<stdlib.h>#include<assert.h>int main(){ char *strcpy(char *strDes, const char *strSrc); char src[20] = "Tomato"; char des[20] = "Potato"; printf("Des: %s\tSrc: %s",des,src); strcpy(des,src); printf("\n"); printf("Des: %s\tSrc: %s",des,src); system("pause");}char *strcpy(char *strDes, const char *strSrc){ char *address = strDes; assert(((strDes != NULL) && (strSrc != NULL))); while((*strDes++ = *strSrc++) != '\0') ; return address;}
运行结果:
2、strcnpy
strncpy 函数 :
char *strncpy(char *strDes,const char *strSrc,int count){ char *address = strDes; assert(strSrc != NULL && strDes != NULL); while(count-- && *strSrc != '\0') *strDes++ = *strSrc++; return address;}
strncpy 函数在vs2012测试例子:
#include<stdio.h>#include<assert.h>#include<stdlib.h>int main(){ char *strncpy(char *strDes,const char *strSrc,int count); char src[20] = "abcdefghi"; char des[20] = "123456789"; printf("Des: %s\tSrc: %s",des,src); strncpy(des,src,5);; printf("\n"); printf("Des: %s\tSrc: %s",des,src); system("pause");}char *strncpy(char *strDes,const char *strSrc,int count){ char *address = strDes; assert(strSrc != NULL && strDes != NULL); while(count-- && *strSrc != '\0') *strDes++ = *strSrc++; return address;}
运行结果:
3、strcat
strcat 函数 :
char *strcat(char *strDes, const char *strSrc){ char *address = strDes; assert((strDes != NULL) && (strSrc != NULL)); while(*strDes != '\0') ++ strDes; while((*strDes ++ = *strSrc++) != '\0') NULL; return address;}
strcat 函数在vs2012测试例子:
#include<stdio.h>#include<assert.h>#include<stdlib.h>int main(){ char *strcat(char *strDes, const char *strSrc); char src[20] = "melon"; char des[20] = "water"; printf("Des: %s\t\tSrc: %s",des,src); strcat(des,src); printf("\n"); printf("Des: %s\t\tSrc: %s",des,src); system("pause");}char *strcat(char *strDes, const char *strSrc){ char *address = strDes; assert((strDes != NULL) && (strSrc != NULL)); while(*strDes != '\0') ++ strDes; while((*strDes ++ = *strSrc++) != '\0') NULL; return address;}
运行结果:
4、strncat
strncat 函数 :
char *strncat(char *strDes, const char *strSrc, int count){ char *address = strDes; assert(strDes != NULL && strSrc != NULL); while(*strDes != '\0') ++strDes; while(count-- && *strSrc != '\0') *strDes++ = *strSrc++; *strDes = '\0'; return address;}
strncat 函数在vs2012测试例子:
#include<stdio.h>#include<assert.h>#include<stdlib.h>int main(){ char *strncat(char *strDes, const char *strSrc, int count); char src[20] = "marksman"; char des[20] = "water"; printf("Des: %s\t\tSrc: %s",des,src); strncat(des,src,4); printf("\n"); printf("Des: %s\t\tSrc: %s",des,src); system("pause");}char *strncat(char *strDes, const char *strSrc, int count){ char *address = strDes; assert(strDes != NULL && strSrc != NULL); while(*strDes != '\0') ++strDes; while(count-- && *strSrc != '\0') *strDes++ = *strSrc++; *strDes = '\0'; return address;}
运行结果:
5、strcmp
strcmp 函数 :
int strcmp(const char *s, const char *t){ assert(s != NULL && t != NULL); while(*s && *t && *s == *t) { ++s; ++t; } return(*s - *t);}
strcmp 函数在vs2012测试例子:
#include<stdio.h>#include<assert.h>#include<stdlib.h>int main(){ int strcmp(const char *s, const char *t); char ss[20] = "eggplant"; char tt[20] = "eggshell"; int result = 0; printf("ss:%s\ntt:%s",ss,tt); result = strcmp(ss,tt); printf("\n"); printf("the compare result is : %d",result); system("pause");}int strcmp(const char *s, const char *t){ assert(s != NULL && t != NULL); while(*s && *t && *s == *t) { ++s; ++t; } return(*s - *t);}
运行结果:
6、strncmp
strcmp 函数 :
int strncmp(const char *s, const char *t, int count){ assert((s != NULL) && (t != NULL)); while(*s && *t && *s == *t && count --) { ++ s; ++ t; } return(*s - *t);}
strcmp 函数在vs2012测试例子:
#include<stdio.h>#include<assert.h>#include<stdlib.h>int main(){ int strncmp(const char *s, const char *t, int count); char ss[] = "eggplant"; char tt[] = "eggshell"; int result = -1; printf("ss:%s\ntt:%s",ss,tt); result = strncmp(ss,tt,2); printf("\n"); printf("the compart result is : %d",result); system("pause");}int strncmp(const char *s, const char *t, int count){ assert((s != NULL) && (t != NULL)); while(*s && *t && *s == *t && count --) { ++ s; ++ t; } return(*s - *t);}
运行结果:
7、总结
1、三对字符串函数:字符串复制函数(strcpy,strncpy)、字符串连接函数(strcat,strncat)、字符串比较函数(strcmp,strncmp);
2、字符串复制函数和字符串连接函数返回值类型是 char*,而字符串比较函数返回值类型是int.
0 0
- 常见的c字符串处理函数的源代码以及简单例子【1】
- 常见的c字符串处理函数的源代码以及简单例子【2】
- 常见的C字符串处理函数的源代码
- 常见的C字符串处理函数的源代码
- 常见的C字符串处理函数的源代码
- 常见的C字符串处理函数的源代码
- 常见的C字符串处理函数的源代码
- 常见的C字符串处理函数的源代码
- 常见的C字符串处理函数的源代码
- C/C++常见字符串处理函数源代码
- C语言常见的字符串处理函数
- c语言常见的字符串处理函数
- C库函数字符串处理函数的C实现(常见)
- C语言中常见的字符串处理函数
- C语言实现常见的字符串处理函数
- C语言中数组以及处理字符串的函数
- C源代码的例子
- C语言字符串处理的一个例子
- 调用cocos Studio生成的动画
- 深入理解C++中的智能指针auto_ptr
- 【Easyui】Easyui Datagrid 排序
- 拒绝围观效应,逆向思维稳定网站排名
- CXX0017:Error:symbol "xxx" not found
- 常见的c字符串处理函数的源代码以及简单例子【1】
- POJ 1308 Is It A Tree?
- Fastnews最佳杂志网站模板
- poj 3678 2-SAT (Katu Puzzle)
- 性能分析十八招,你用哪一招
- Oracle实现多表插入-INSERT ALL
- listView使用中的一些技巧
- Fabrity - 的Pligg内容管理系统
- vs2010上安装opencv2.49