C语言之strstr函数

来源:互联网 发布:unity3d eventsystem 编辑:程序博客网 时间:2024/06/05 17:44

FROM MSDN && 百科】

原型:char *strstr(const char *str1, const char *str2);

#include<string.h>

找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。

Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear instr. If strSearch points to a string of zero length, the function returns str.

DEMO: mystrstr



[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. #include <string.h>  
  4. #include <stdlib.h>  
  5. #pragma warning (disable:4996)  
  6. char *mystrstr(char *s1,char *s2);  
  7. int main(void)  
  8. {  
  9.     char *s="Golden Global View";  
  10.     char *l="ob";   //char *l=""  
  11.     char *p;  
  12.     system("cls");  
  13.     p=mystrstr(s,l);  
  14.     if (p!=NULL)  
  15.     {  
  16.         printf("%s\n",p);  
  17.     }  
  18.     else  
  19.     {  
  20.         printf("Not Found!\n");  
  21.     }  
  22.     getch();  
  23.     return 0;  
  24. }  
  25. /*FROM 百科*/  
  26. char *mystrstr(char *s1,char *s2)  
  27. {  
  28.     int n;  
  29.     if (*s2)                      //两种情况考虑  
  30.     {  
  31.         while(*s1)                 
  32.         {  
  33.             for (n=0;*(s1+n)==*(s2+n);n++)  
  34.             {  
  35.                 if (!*(s2+n+1))            //查找的下一个字符是否为'\0'  
  36.                 {  
  37.                     return (char*)s1;  
  38.                 }  
  39.             }  
  40.             s1++;  
  41.         }  
  42.         return NULL;  
  43.     }  
  44.     else  
  45.     {  
  46.         return (char*)s1;  
  47.     }  
  48. }  

DEMO:

[cpp] view plain copy
  1. //#define FIRST_DEMO  
  2. #define SECOND_DEMO  
  3.   
  4. #ifdef FIRST_DEMO  
  5. #include <stdio.h>  
  6. #include <conio.h>  
  7. #include <string.h>  
  8. int main(void)  
  9. {  
  10.     char *s="Golden Global View";  
  11.     char *l="lob";  
  12.     char *p;  
  13.     system("cls");  
  14.     p=strstr(s,l);  
  15.     if (p!=NULL)  
  16.     {  
  17.         printf("%s\n",p);  
  18.     }  
  19.     else  
  20.     {  
  21.         printf("Not Found!\n");  
  22.     }  
  23.   
  24.     getch();  
  25.     return 0;  
  26. }  
  27. #elif defined SECOND_DEMO  
  28. /*从字串” string1 onexxx string2 oneyyy”中寻找”yyy”*/  
  29. #include <stdio.h>  
  30. #include <conio.h>  
  31. #include <string.h>  
  32. int main(void)  
  33. {  
  34.     char *s="string1 onexxx string2 oneyyy";  
  35.     char *p;  
  36.     p=strstr(s,"string2");  
  37.     printf("%s\n",p);  
  38.     if (p==NULL)  
  39.     {  
  40.         printf("Not Found!\n");  
  41.     }  
  42.     p=strstr(p,"one");  
  43.     printf("%s\n",p);  
  44.     if (p==NULL)  
  45.     {  
  46.         printf("Not Found!\n");  
  47.     }  
  48.     p+=strlen("one");  
  49.     printf("%s\n",p);  
  50.   
  51.     getch();  
  52.     return 0;  
  53. }  
  54. #endif  
0 0
原创粉丝点击