C++ str系列函数 (包含strtok用法)

来源:互联网 发布:mysql update 多表 编辑:程序博客网 时间:2024/06/04 23:21

1、strcat()

此函数原型为 char *strcat(char *dest, const char *src).
功能为连接两个字符串,把src连接到dest后面;返回dest地址
实现如下
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char * strcat(char *dest,const char *src)  
  2. {  
  3.     char* addr=dest;  
  4.     while(*dest)//找到'\0'  
  5.     {  
  6.         dest++;  
  7.     };  
  8.     while(*dest++=*src++)  
  9.     {};  
  10.     return addr;  
  11. }  

2、strcmp()

此函数的函数原型为 int strcmp(const char *str1, const char *str2).
功能为比较两个字符串。
当str1指向的字符串大于str2指向的字符串时,返回正数。
当str1指向的字符串等于str2指向的字符串时,返回0。
当str1指向的字符串小于str2指向的字符串时,返回负数。
实现如下:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int strcmp(const char *str1, const char *str2)  
  2. {  
  3.     while(*str1==*str2)  
  4.     {  
  5.         if(*str1=='\0')  
  6.             return 0;  
  7.         str1++;  
  8.         str2++;  
  9.     }  
  10.     return *str1-*str2;  
  11. }  

3、strcpy()

此函数原型为 char *strcpy(char* dest, const char *src)
功能为拷贝字符串内容到目的串,把src所指向的内容拷贝到dest
实现如下
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strcpy(char *dest,const char *src)  
  2. {  
  3.     //assert(dest!=NULL&&src!=NULL);  
  4.     char *addr=dest;  
  5.     while(*dest++=*src++);  
  6.     return addr;  
  7. }  
 

4、strlen()  

此函数原型为unsigned in strlen(const char *str)

功能为返回字符串str的长度(不包括'\0')。

实现如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. unsigned int strlen(const char *str)  
  2. {  
  3.     unsigned len=0;  
  4.     while(*str!='\0')  
  5.     {  
  6.         len++;  
  7.         str++;  
  8.     }  
  9.     return len;  
  10. }  

 

5、strchr()  strrchr()

char *strchr(char *str, char c)

功能为查找str中首次出现c的位置,如有有,则返回出现位置,否则返回NULL。实现如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strchr(char *str, char c)  
  2. {  
  3.     while(*str!='\0'&&*str!=c)  
  4.     {  
  5.         str++;  
  6.     }  
  7.     return (*str==c? str: NULL);  
  8. }  

char *strrchr(char *str, char c)

功能为查找str中最后一次出现c的位置,如有有,则返回出现位置,否则返回NULL。实现如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strrchr(char *str, char c)  
  2. {  
  3.     char *p=str+strlen(str);//p指向最后一个字符  
  4.     while(p!=str&&*p!=c)  
  5.     p--;  
  6.     if(p==str&&*p!=c)  
  7.         return NULL;  
  8.     else return p;  
  9. }  

 

6、strcspn()  strspn()

strcspn

原型:size_t strcspn(const char *pstr, const char *strCharset)

MSDN解释为:在字符串pstr中搜寻strCharsret中所出现的字符,返回strCharset中出现的第一个字符在pstr中的出现位置。简单的说,若strcspn返回的数值为n,则代表字符串strCharsrt开头连续有n个字符不包含在pstr内的字符。

实现十分巧妙,在http://blog.csdn.net/chenyu2202863/article/details/5293941

原型size_t strspn(const char *pstr, const char *strCharset)

功能:返回后面字符串中第一个不在前者出现的下表。 

7、strdup()

此函数原型为char *strdup(const char *str)
功能为拷贝字符串到新建的内存,返回内存指针。若失败,返回NULL。要注意,返回的指针指向的内存在堆中,所以要手动释放。
函数实现:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strdup(const char *str)  
  2. {  
  3.     char *p=NULL;  
  4.     if(str&&(p=(char*)malloc(strlen(str)+1)))  
  5.         strcpy(p,str);  
  6.     return p;  

8、strrev()

此函数的原型为char *strrev(char *str)
功能为反转字符串,返回字符串指针。
函数实现:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strrev(char *str)  
  2. {  
  3.     if(str==NULL)  
  4.         return NULL;  
  5.     char *start=str;  
  6.     char *end=str+strlen(str)-1;  
  7.     char temp;  
  8.     while(start<end)  
  9.     {  
  10.         temp=*start;  
  11.         *start=*end;  
  12.         *end=temp;  
  13.         start++;  
  14.         end--;  
  15.     }  
  16.     return str;  
  17. }  

9、strstr()

函数原型为char *strstr(const char str1, const char *str2)
功能为查找字符串str2在str1中出现的位置,找到则返回位置,否则返回NULL。
函数实现:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strstr(const char str1, const char *str2)  
  2. {  
  3.     int length1=strlen(str1);  
  4.     int length2=strlen(str2);  
  5.     while(length1>=length2)  
  6.     {  
  7.         length1--;  
  8.         if(!strncpy(str1,str2,length2))//比较前n个字符串,类似strcpy  
  9.             return str1;  
  10.         str1++;  
  11.     }  
  12.     return NULL;  
  13. }  

10、strtok()

函数原型为char *strtok(char s[], const char *delim);
头文件《string》
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。

例子:
  1. int in=0;  
  2. char buffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";      
  3. char *p[20];  
  4. char *buf = buffer;  
  5. while((p[in]=strtok(buf,","))!=NULL)   
  6. {  

  7.     in++;  
  8.     buf=NULL;  
  9. }  
  10. printf("Here we have %d strings/n", in);  
  11. for (int j=0; j<in; j++)  
  12. {     
  13.     printf(">%s</n",p[j]);  
  14. }  
特别要注意一点:不要把char【】扔进去要把char*扔进去,因为之后要把指针赋值为null,所以必须要指针。
用法其实跟getline很像,不断把分出来的块扔到你的指针里面。配合while还是很好用的。
只不过要把原指针指向NULL,这点千万别忘。

另附上一个例子,这个代码风格也是极好的:

[cpp] view plaincopy
  1. /* strtok example */  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. int main ()  
  6. {  
  7.   char str[] ="- This, a sample string.";  
  8.   char * pch;  
  9.   printf ("Splitting string \"%s\" into tokens:\n",str);  
  10.   pch = strtok (str," ,.-");  
  11.   while (pch != NULL)  
  12.   {  
  13.     printf ("%s\n",pch);  
  14.     pch = strtok (NULL, " ,.-");  
  15.   }  
  16.   return 0;  
  17. }  

0 0
原创粉丝点击