Chinaunix首页 | 论坛 | 博客

Go

  • 博客访问: 220298
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 783
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-12 16:29
文章分类

全部博文(67)

文章存档

2015年(1)

2014年(47)

2013年(19)

我的朋友

分类: C/C++

2014-08-07 17:24:02

  1. 转载:http://blog.csdn.net/sunnylgz/article/details/6677103 


    PS:本文包含了大部分strings函数的说明,并附带举例说明。本来想自己整理一下的,发现已经有前辈整理过了,就转了过来。修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时崩溃。另外自己重写了部分测试程序,使其更能满足自己测试的需要。不当之处,还请海涵。

    @函数原型:  char *strdup(const char *s) 

    函数功能:  字符串拷贝,目的空间由该函数分配  

    函数返回:  指向拷贝后的字符串指针 

    参数说明:  src-待拷贝的源字符串 

    所属文件:  


    1. #include    
    2. #include    
    3. #include    
    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的指针 

    参数说明: 

    所属文件:  


    1. #include    
    2. #include    
    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-拷贝的字符个数 

    所属文件:  


    1. #include   
    2. #include   
    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. }  
    输出:



    1. /******************************************* 
    2. aaaaaaaaaaaaaaaaaaaa 
    3. bbbbbbbbbbaaaaaaaaaa 
    4. *******************************************/  
    注意:strncpy只复制指定长度的字符,不会自动在末尾加'\0'。若指定长度超过源字符串长度,不够的部分补‘\0’,


     

    @函数名称:  strcat 

    函数原型:  char* strcat(char * str1,char * str2); 

    函数功能:  把字符串str2接到str1后面,str1最后的'\0'被取消 

    函数返回:  str1 

    参数说明: 

    所属文件:  


    1. #include    
    2. #include   
    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中 

    函数返回: 

    参数说明: 

    所属文件:  


    1. #include    
    2. #include   
    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. }  
    注意:与strncpy不同的是,strncat会自动在末尾加‘\0’,若指定长度超过源字符串长度,则只复制源字符串长度即停止


     

    @函数名称:  strcmp 

    函数原型:  int strcmp(char * str1,char * str2); 

    函数功能:  比较两个字符串str1,str2. 

    函数返回:  str1str2,返回正数.  

    参数说明: 

    所属文件:  


    1. #include    
    2. #include    
    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:str1str2 

    参数说明:  str1,str2-待比较的字符串,count-比较的长度 

    所属文件:  


    1. #include   
    2. #include   
    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. }  

    输出:


    1. /**************************************** 
    2. 1:str1= str2 
    3. 3:str1< str2 
    4. *****************************************/  



     

    @函数名称:  strpbrk 

    函数原型:  char *strpbrk(const char *s1, const char *s2) 

    函数功能:  得到s1中第一个“同时也出现在s2中”字符的位置指针 

    函数返回:  位置指针 

    参数说明: 

    所属文件:  


    1. #include   
    2. #include   
    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. }  

    输出:


    1. /************************************** 
    2. ind all vowels 
    3. all vowels 
    4. owels 
    5. els 
    6. **************************************/  



     

    @函数名称:  strcspn 

    函数原型:  int strcspn(const char *s1, const char *s2) 

    函数功能:  统计s1中从头开始直到第一个“来自s2中的字符”出现的长度 

    函数返回:  长度 

    参数说明: 

    所属文件:  


    1. #include   
    2. #include  
    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. }  

    输出:


    1. /************************ 
    2. 0 
    3. 3 
    4. 9 
    5. ************************/  



     

    @函数名称:  strspn 

    函数原型:  int strspn(const char *s1, const char *s2) 

    函数功能:  统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度 

    函数返回:  位置指针 

    参数说明: 

    所属文件:  


    [html] view plaincopy
    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. }  
    输出:



    1. /************************ 
    2. 6 
    3. 0 
    4. 0 
    5. ************************/  


     

    @函数名称:  strchr 

    函数原型:  char* strchr(char* str,char ch); 

    函数功能:  找出str指向的字符串中第一次出现字符ch的位置 

    函数返回:  返回指向该位置的指针,如找不到,则返回空指针 

    参数说明:  str-待搜索的字符串,ch-查找的字符 

    所属文件:  


    1. #include   
    2. #include   
    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. }  

    运行结果:


    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字符的位置指针 

    函数返回:  位置指针 

    参数说明: 

    所属文件:  


    1. #include   
    2. #include   
    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的串结束符) 

    函数返回:  返回该位置的指针,如找不到,返回空指针 

    参数说明: 

    所属文件:  


    1. #include   
    2. #include   
    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的指针  

    参数说明: 

    所属文件:  


    1. #include   
    2. #include   
    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. }  
    输出:



    1. /************************************ 
    2. Beforestrrev():string 
    3. After strrev():gnirts 
    4. ************************************/  



     

    @函数名称:  strnset 

    函数原型:  char *strnset(char *s, int ch, size_t n) 

    函数功能:  将字符串s中前n个字符设置为ch的值 

    函数返回:  指向s的指针 

    参数说明: 

    所属文件:  


    1. #include   
    2. #include   
    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. }  

    输出:


    1. /************************************************* 
    2. string beforestrnset: aaaaaaaaaaaaaaaaaaaaaaa 
    3. string afterstrnset:  xxxxxxxxxxaaaaaaaaaaaaa 
    4. *************************************************/  



     

    @函数名称:  strset 

    函数原型:  char *strset(char *s, int ch) 

    函数功能:  将字符串s中所有字符设置为ch的值 

    函数返回:  指向s的指针  

    参数说明: 

    所属文件:  


    1. #include   
    2. #include   
    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被破坏了 

            函数会记忆指针位置以供下一次调用 

             

    所属文件:  


    1. #include   
    2. #include   
    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中的字符变为大写 

    函数返回: 

    参数说明: 

    所属文件:  


    1. #include    
    2. #include    
    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的指针 

    参数说明: 

    所属文件:  


    1. #include   
    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-错误编号 

    所属文件:  


    1. #include    
    2. #include    
    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-拷贝的最大长度 

    所属文件:  ,


    1. #include    
    2. #include    
    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. }  
    输出:



    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-拷贝底目的字符串指针 

    所属文件:  ,


    1. #include    
    2. #include    
    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. }  
    输出:



    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-指定长度 

    所属文件:  ,


    1. #include    
    2. #include    
    3. int main()   
    4. {   
    5.   char str[17];   
    6.   char *ptr;   
    7.   strcpy(str,"This is a string");   
    8.   ptr=memchr(str,'r',strlen(str));   
    9.   if (ptr)   
    10.   printf("The character 'r' is at position:%d",ptr-str);   
    11.   else   
    12.   printf("The character was not found");   
    13.   return 0;   
    14. }  



     

    @函数名称:  memcmp 

    函数原型:  int memcmp(const void *s1, const void *s2,size_t n) 

    函数功能:  按字典顺序比较两个串s1和s2的前n个字节  

    函数返回:  <0,=0,>0分别表示s1<,=,>s2 

    参数说明:  s1,s2-要比较的字符串,n-比较的长度 

    所属文件:  ,


    1. #include    
    2. #include    
    3. int main()    
    4. {    
    5.   char *buf1="ABCDE123";    
    6.   char *buf2="abcde456";    
    7.   int stat;    
    8.   stat=memcmp(buf1,buf2,5);    
    9.   printf("The strings to position 5 are");    
    10.   if(stat) printf("not ");    
    11.   printf("the same\n");    
    12.   return 0;    
    13. }   



     

    @函数名称:  memicmp 

    函数原型:  int memicmp(const void *s1, const void *s2, size_t n) 

    函数功能:  按字典顺序、不考虑字母大小写对字符串s1,s2前n个字符比较 

    函数返回:  <0,=0,>0分别表示s1<,=,>s2 

    参数说明:  s1,s2-要比较的字符串,n-比较的长度 

    所属文件:  ,


    1. #include    
    2. #include    
    3. int main()   
    4. {   
    5.   char *buf1="ABCDE123";   
    6.   char *buf2="abcde456";   
    7.   int stat;   
    8.   stat=memicmp(buf1,buf2,5);   
    9.   printf("The strings to position 5 are");   
    10.   if(stat) printf("not");   
    11.   printf("the same");   
    12.   return 0;   
    13. }  
    输出:



    1. /************************************** 
    2. The strings to position 5 are the same 
    3. ***************************************/  



     

    @函数名称:  memmove 

    函数原型:  void *memmove(void *dest, const void *src, size_t n) 

    函数功能:  字符串拷贝 

    函数返回:  指向dest的指针 

    参数说明:  src-源字符串,n-拷贝的最大长度 

    所属文件:  ,


    1. #include    
    2. #include    
    3. int main()   
    4. {   
    5.   chardest[40]="abcdefghijklmnopqrstuvwxyz0123456789";   
    6.   printf("destination prior tomemmove:%s\n",dest);   
    7.   memmove(dest+1,dest,35);   
    8.   printf("destination aftermemmove:%s",dest);   
    9.   return 0;   
    10. }  


    PS:与memcpy不同的是,memmove可以处理目的字符串与源字符串地址空间出现重叠的情况,可保证待复制的内容不被破坏。


    @函数名称:   memset

    函数原型:   void *memset(void *s, int c, size_t n)

    函数功能:   字符串中的n个字节内容设置为c

    函数返回:

    参数说明:   s-要设置的字符串,c-设置的内容,n-长度

    所属文件:   ,


    1. #include   
    2. #include   
    3. #include   
    4. int main()  
    5. {  
    6.   charbuffer[]="Hello world";  
    7.  printf("Buffer before memset:%s/n",buffer);  
    8.  memset(buffer,'*',strlen(buffer)-1);  
    9.  printf("Buffer after memset:%s",buffer);  
    10.   return 0;  
    11. }  


阅读(615) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~