设为首页 加入收藏

TOP

c语言中字符串处理c语言中字符串处理函数(一)
2014-11-23 22:25:46 来源: 作者: 【 】 浏览:1
Tags:言中 字符串 处理 函数

@函数名称: strdup
函数原型: char *strdup(const char *s)
函数功能: 字符串拷贝,目的空间由该函数分配
函数返回: 指向拷贝后的字符串指针
参数说明: src-待拷贝的源字符串
所属文件:
#include
#include
#include
int main()
{
char *dup_str, *string="abcde";
dup_str=strdup(string);
printf("%s", dup_str);
free(dup_str);
return 0;
}

@函数名称: strcpy
函数原型: char* strcpy(char* str1,char* str2);
函数功能: 把str2指向的字符串拷贝到str1中去
函数返回: 返回str1,即指向str1的指针
参数说明:
所属文件:
#include
#include
int main()
{
char string[10];
char *str1="abcdefghi";
strcpy(string,str1);
printf("the string is:%s ",string);
return 0;
}

@函数名称: strncpy
函数原型: char *strncpy(char *dest, const char *src,int count)
函数功能: 将字符串src中的count个字符拷贝到字符串dest中去
函数返回: 指向dest的指针
参数说明: dest-目的字符串,src-源字符串,count-拷贝的字符个数
所属文件:
#include
#include
int main()
{
char string[10];
char *str1="abcdefghi";
strncpy(string,str1,3);
string[3]=;
printf("%s",string);
return 0;
}

@函数名称: strcat
函数原型: char* strcat(char * str1,char * str2);
函数功能: 把字符串str2接到str1后面,str1最后的被取消
函数返回: str1
参数说明:
所属文件:
#include
#include
int main()
{
char buffer[80];
strcpy(buffer,"Hello ");
strcat(buffer,"world");
printf("%s ",buffer);
return 0;
}

@函数名称: strncat
函数原型: char *strncat(char *dest, const char *src, size_t maxlen)
函数功能: 将字符串src中前maxlen个字符连接到dest中
函数返回:
参数说明:
所属文件:
#include
#include
char buffer[80];
int main()
{
strcpy(buffer,"Hello ");
strncat(buffer,"world",8);
printf("%s ",buffer);
strncat(buffer,"*************",4);
printf("%s ",buffer);
return 0;
}

@函数名称: strcmp
函数原型: int strcmp(char * str1,char * str2);
函数功能: 比较两个字符串str1,str2.
函数返回: str1str2,返回正数.
参数说明:
所属文件:
#include
#include
int main()
{
char *buf1="aaa", *buf2="bbb", *buf3="ccc";
int ptr;
ptr=strcmp(buf2, buf1);
if(ptr>0)
printf("buffer 2 is greater than buffer 1 ");
else
printf("buffer 2 is less than buffer 1 ");
ptr=strcmp(buf2, buf3);
if(ptr>0)
printf("buffer 2 is greater than buffer 3 ");
else
printf("buffer 2 is less than buffer 3 ");
return 0;
}

@函数名称: strncmp
函数原型: int strncmp(char *str1,char *str2,int count)
函数功能: 对str1和str2中的前count个字符按字典顺序比较
函数返回: 小于0:str1str2
参数说明: str1,str2-待比较的字符串,count-比较的长度
所属文件:
#include
#include
int main()
{
int ptr;
char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";
ptr=strncmp(buf2,buf1,3);
if (ptr>0)
printf("buffer 2 is greater than buffer 1");
else
printf("buffer 2 is less than buffer 1");
ptr=strncmp(buf2,buf3,3);
if (ptr>0)
printf("buffer 2 is greater than buffer 3");
else
printf("buffer 2 is less than buffer 3");
return(0);
}

@函数名称: strpbrk
函数原型: char *strpbrk(const char *s1, const char *s2)
函数功能: 得到s1中第一个“同时也出现在s2中”字符的位置指针
函数返回: 位置指针
参数说明:
所属文件:
#include
#include
int main()
{
char *p="Find all vowels";
while(p)
{
printf("%s ",p);
p=strpbrk(p+1,"aeiouAEIOU");
}
return 0;
}

@函数名称: strcspn
函数原型: int strcspn(const char *s1, const char *s2)
函数功能: 统计s1中从头开始直到第一个“来自s2中的字符”出现的长度
函数返回: 长度
参数说明:
所属文件:
#include
#include
int main()
{
printf("%d ",strcspn("abcbcadef","cba"));
printf("%d ",strcspn("xxxbcadef","cba"));
printf("%d ",strcspn("123456789","cba"));
return 0;
}

@函数名称: strspn
函数原型: int strspn(const char *s1, const char *s2)
函数功能: 统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度
函数返回: 位置指针
参数说明:
所属文件:
#include
#include
#include
int main()
{
printf("%d ",strspn("out to lunch","aeiou"));
printf("%d ",strspn("out to lunch","xyz")

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言动态库转静态库不爽记 下一篇C语言的大数相加

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: