字符串处理函数大全(三)

来源:互联网 发布:淘宝图形工作站能买吗 编辑:程序博客网 时间:2024/05/10 13:05
strchr
  原型:extern char *strchr(char *s,char c);          用法:#include <string.h>    功能:查找字符串s中首次出现字符c的位置    说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。    举例:      // strchr.c            #include <syslib.h>      #include <string.h>      main()      {        char *s="Golden Global View";        char *p;                clrscr();                strchr(s,'V');        if(p)          printf("%s",p);        else          printf("Not Found!");        getchar();        return 0;      }        相关函数:memchr
  strcmp
  原型:extern int strcmp(char *s1,char * s2);          用法:#include <string.h>    功能:比较字符串s1和s2。    说明:        当s1<s2时,返回值<0        当s1=s2时,返回值=0        当s1>s2时,返回值>0    举例:      // strcmp.c            #include <syslib.h>      #include <string.h>      main()      {        char *s1="Hello, Programmers!";        char *s2="Hello, programmers!";        int r;                clrscr();                r=strcmp(s1,s2);        if(!r)          printf("s1 and s2 are identical");        else        if(r<0)          printf("s1 less than s2");        else          printf("s1 greater than s2");                getchar();        return 0;      }        相关函数:bcmp,memcmp,stricmp,strncmp
stricmp,strcmpi
  原型:extern int stricmp(char *s1,char * s2);          用法:#include <string.h>    功能:比较字符串s1和s2,但不区分字母的大小写。    说明:strcmpi是到stricmp的宏定义,实际未提供此函数。        当s1<s2时,返回值<0        当s1=s2时,返回值=0        当s1>s2时,返回值>0    举例:      // stricmp.c            #include <syslib.h>      #include <string.h>      main()      {        char *s1="Hello, Programmers!";        char *s2="Hello, programmers!";        int r;                clrscr();                r=stricmp(s1,s2);        if(!r)          printf("s1 and s2 are identical");        else        if(r<0)          printf("s1 less than s2");        else          printf("s1 greater than s2");                getchar();        return 0;      }        相关函数:bcmp,strcmp,stricmp
strcpy
  原型:extern char *strcpy(char *dest,char *src);          用法:#include <string.h>    功能:把src所指由NULL结束的字符串复制到dest所指的数组中。    说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。        返回指向dest的指针。    举例:      // strcpy.c            #include <syslib.h>      #include <string.h>      main()      {        char *s="Golden Global View";        char d[20];                clrscr();                strcpy(d,s);        printf("%s",d);        getchar();        return 0;      }        相关函数:memccpy,memcpy,stpcpy,strdup
strcspn
  原型:extern int strcspn(char *s1,char *s2);          用法:#include <string.h>    功能:在字符串s1中搜寻s2中所出现的字符。    说明:返回第一个出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度。    举例:      // strcspn.c            #include <syslib.h>      #include <string.h>      main()      {        char *s="Golden Global View";        char *r="new";        int n;                clrscr();                n=strcspn(s,r);        printf("The first char both in s1 and s2 is: %c",s[n]);                getchar();        return 0;      }        相关函数:strpbrk
strdup
  原型:extern char *strdup(char *s);          用法:#include <string.h>    功能:复制字符串s    说明:返回指向被复制的字符串的指针,所需空间由malloc()分配且可以由free()释放。    举例:      // strdup.c            #include <syslib.h>      #include <string.h>      main()      {        char *s="Golden Global View";        char *d;                clrscr();                d=strdup(s);        printf("%s",d);        getchar();        return 0;      }        相关函数:memccpy,memcpy,strcpy
stricmp,strcmpi
  原型:extern int stricmp(char *s1,char * s2);          用法:#include <string.h>    功能:比较字符串s1和s2,但不区分字母的大小写。    说明:strcmpi是到stricmp的宏定义,实际未提供此函数。        当s1<s2时,返回值<0        当s1=s2时,返回值=0        当s1>s2时,返回值>0    举例:      // stricmp.c            #include <syslib.h>      #include <string.h>      main()      {        char *s1="Hello, Programmers!";        char *s2="Hello, programmers!";        int r;                clrscr();                r=stricmp(s1,s2);        if(!r)          printf("s1 and s2 are identical");        else        if(r<0)          printf("s1 less than s2");        else          printf("s1 greater than s2");                getchar();        return 0;      }        相关函数:bcmp,strcmp,stricmp
strlen
  原型:extern int strlen(char *s);          用法:#include <string.h>    功能:计算字符串s的长度    说明:返回s的长度,不包括结束符NULL。    举例:      // strlen.c            #include <syslib.h>      #include <string.h>      main()      {        char *s="Golden Global View";                clrscr();                printf("%s has %d chars",s,strlen(s));        getchar();        return 0;      }        相关函数:无
strlwr
  原型:extern char *strlwr(char *s);          用法:#include <string.h>    功能:将字符串s转换为小写形式    说明:只转换s中出现的大写字母,不改变其它字符。返回指向s的指针。    举例:      // strlwr.c            #include <syslib.h>      #include <string.h>      main()      {        char *s="Copywrite 1999-2000 GGV Technologies";                clrscr();                printf("%s",strlwr(s));        getchar();        return 0;      }        相关函数:strupr
strncat
  原型:extern char *strncat(char *dest,char *src,int n);          用法:#include <string.h>    功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'/0')并添加'/0'。    说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。        返回指向dest的指针。    举例:      // strncat.c            #include <syslib.h>      #include <string.h>      main()      {        char d[20]="Golden Global";        char *s=" View WinIDE Library";                clrscr();                strncat(d,s,5);        printf("%s",d);        getchar();        return 0;      }        相关函数:strcat