资源描述:
《c 字符串操作函数》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C字符串操作函数函数名:stpcpy功能:拷贝一个字符串到另一个用法:char*stpcpy(char*destin,char*source);程序例:#include#includeintmain(void){charstring[10];char*str1="abcdefghi";stpcpy(string,str1);printf("%s",string);return0;}函数名:strcat功能:字符串拼接函数用法:char*strcat(char*destin,char*source
2、);程序例:#include#includeintmain(void){chardestination[25];char*blank="",*c="C++",*Borland="Borland";strcpy(destination,Borland);strcat(destination,blank);strcat(destination,c);printf("%s",destination);return0;}函数名:strchr功能:在一个串中查找给定字符的第一个匹配之处用法:char*s
3、trchr(char*str,charc);程序例:#include#includeintmain(void){charstring[15];char*ptr,c='r';strcpy(string,"Thisisastring");ptr=strchr(string,c);if(ptr)printf("Thecharacter%cisatposition:%d",c,ptr-string);elseprintf("Thecharacterwasnotfound");return0;}函数名
4、:strcmp功能:串比较用法:intstrcmp(char*str1,char*str2);看Asic码,str1>str2,返回值>0;两串相等,返回0程序例:#include#includeintmain(void){char*buf1="aaa",*buf2="bbb",*buf3="ccc";intptr;ptr=strcmp(buf2,buf1);if(ptr>0)printf("buffer2isgreaterthanbuffer1");elseprintf("buffer2isl
5、essthanbuffer1");ptr=strcmp(buf2,buf3);if(ptr>0)printf("buffer2isgreaterthanbuffer3");elseprintf("buffer2islessthanbuffer3");return0;}函数名:strncmpi功能:将一个串中的一部分与另一个串比较,不管大小写用法:intstrncmpi(char*str1,char*str2,unsignedmaxlen);程序例:#include#includeintm
6、ain(void){char*buf1="BBB",*buf2="bbb";intptr;ptr=strcmpi(buf2,buf1);if(ptr>0)printf("buffer2isgreaterthanbuffer1");if(ptr<0)printf("buffer2islessthanbuffer1");if(ptr==0)printf("buffer2equalsbuffer1");return0;}函数名:strcpy功能:串拷贝用法:char*strcpy(char*str1,char*str2);程序例:
7、#include#includeintmain(void){charstring[10];char*str1="abcdefghi";strcpy(string,str1);printf("%s",string);return0;}函数名:strcspn功能:在串中查找第一个给定字符集内容的段用法:intstrcspn(char*str1,char*str2);程序例:#include#include#includeintmain(void
8、){char*string1="1234567890";char*string2="747DC8";intlength;length=strcspn(string1,string2);printf("Cha