字符串函数---strstr()、memchr()、strchr()详解及实现

来源:互联网 发布:秒杀软件利剑 编辑:程序博客网 时间:2024/05/16 10:44

原文出处:http://blog.csdn.net/lanzhihui_10086/article/details/39831935

一、strstr()函数:

    strstr():搜索一个字符串在另一个字符串中的第一次出现。找到所搜索的字符串,则该函数返回第一次匹配的字符串的地址;如果未找到所搜索的字符串,则返回NULL。

    格式:strstr(str1,str2);

     str1: 被查找目标

     str2: 要查找对象

 

实现代码:

[cpp] view plain copy
  1. #include<iostream>  
  2. #include<assert.h>  
  3.   
  4. using namespace std;  
  5.   
  6.   
  7. char *strstr_m(const char *str1,const char *str2)  
  8. {  
  9.     const char *cp=NULL;  
  10.     const char *c=str2;  
  11.     bool falg=false;  
  12.   
  13.     if(*str2=='\0'return (char*)str1;//当第二个参数为空时,返回str1的首地址  
  14.       
  15.     while(*str1!='\0')  
  16.     {  
  17.         while(*str1==*str2)  
  18.         {  
  19.             if(!falg)  
  20.             {  
  21.                 falg=true;  
  22.                 cp=str1;  
  23.             }  
  24.             str1++;  
  25.             str2++;  
  26.             if(*str2=='\0')  
  27.                 return (char*)cp;  
  28.         }  
  29.         str1++;  
  30.         cp=NULL;  
  31.         falg=false;  
  32.         str2=c;  
  33.     }  
  34.     return (char*)cp;  
  35. }  
  36.   
  37. int main()  
  38. {  
  39.     char a[]="lanzhihui is a good boy!";  
  40.   
  41.     char *s=strstr_m(a,"is");//注意:当第二个实参为  ""  时,s指向a数组的首地址  
  42.   
  43.     if(s!=NULL)  
  44.     {  
  45.         cout<<s<<endl;  
  46.     }  
  47.     else  
  48.     {  
  49.         cout<<"Not Find!"<<endl;  
  50.     }  
  51.   
  52.     system("pause");  
  53.     return 0;  
  54. }  

运行上面结果为:is a good boy!

 

二、memchr()函数

    memchr():  void *memchr( const void *buffer, int ch, size_t count );

    函数在buffer指向的数组的count个字符的字符串里查找ch 首次出现的位置。返回一个指针,指向ch 在字符串中首次出现的位置, 如果ch 没有在字符串中找到,返回NULL。

 

实现代码:

[cpp] view plain copy
  1. #include<iostream>  
  2. #include<assert.h>  
  3.   
  4. using namespace std;  
  5.   
  6. void *memchr_m(const void *buffer,int ch,int n)  
  7. {  
  8.     assert(buffer!=NULL);  
  9.   
  10.     char *cp=(char*)buffer;  
  11.   
  12.     while(*cp++!='\0'&&n)  
  13.     {  
  14.         if(*cp-ch==0)  
  15.             return (void*)cp;  
  16.         --n;  
  17.     }  
  18.     return NULL;  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     char a[]="lanzhihui is a good boy!";  
  24.   
  25.     char *p;  
  26.     p=(char*)memchr_m(a,'z',50);  
  27.   
  28.     if(p!=NULL)  
  29.     {  
  30.         cout<<"Find!"<<endl;  
  31.         cout<<p<<endl;  
  32.     }  
  33.     else  
  34.     {  
  35.         cout<<"Not Find!"<<endl;  
  36.     }  
  37.   
  38.     system("pause");  
  39.     return 0;  
  40. }  

 

 

三、strchr()函数

    strchr():char *strchr(const char *s,char c);
    功能:查找字符串s中首次出现字符c的位置
    返回值:成功则返回要查找字符第一次出现的位置,失败返回NULL。

 

实现代码:

[cpp] view plain copy
  1. #include<iostream>  
  2. #include<assert.h>  
  3.   
  4. using namespace std;  
  5.   
  6. char *strchr_m(const char *s,int ch)  
  7. {  
  8.     assert(s!=NULL);  
  9.   
  10.     while(*s!='\0')  
  11.     {  
  12.         if(*s-ch==0)  
  13.             return (char*)s;  
  14.         s++;  
  15.     }  
  16.     return NULL;  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     char a[]="lanzhihui is a good boy!";  
  22.   
  23.     char *p;  
  24.   
  25.     p=strchr_m(a,'l');  
  26.   
  27.     if(p!=NULL)  
  28.     {  
  29.         cout<<"Find!"<<endl;  
  30.         cout<<p<<endl;  
  31.     }  
  32.   
  33.     else  
  34.     {  
  35.         cout<<"Not Find!"<<endl;  
  36.     }  
  37.   
  38.     system("pause");  
  39.     return 0;  
  40. }  
阅读全文
0 0
原创粉丝点击