1. 程式人生 > >char *strstr(const char *str1, const char *str2);

char *strstr(const char *str1, const char *str2);

col ear style inter -s 包括 endif comment mys

【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 in str. If strSearch points to a string of zero length, the function returns str.

DEMO: mystrstr

    1. //#define FIRST_DEMO
    2. #define SECOND_DEMO
    3. #ifdef FIRST_DEMO
    4. #include <stdio.h>
    5. #include <conio.h>
    6. #include <string.h>
    7. int main(void)
    8. {
    9. char *s="Golden Global View";
    10. char *l="lob";
    11. char *p;
    12. system("cls");
    13. p=strstr(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. #elif defined SECOND_DEMO
    26. /*從字串” string1 onexxx string2 oneyyy”中尋找”yyy”*/
    27. #include <stdio.h>
    28. #include <conio.h>
    29. #include <string.h>
    30. int main(void)
    31. {
    32. char *s="string1 onexxx string2 oneyyy";
    33. char *p;
    34. p=strstr(s,"string2");
    35. printf("%s\n",p);
    36. if (p==NULL)
    37. {
    38. printf("Not Found!\n");
    39. }
    40. p=strstr(p,"one");
    41. printf("%s\n",p);
    42. if (p==NULL)
    43. {
    44. printf("Not Found!\n");
    45. }
    46. p+=strlen("one");
    47. printf("%s\n",p);
    48. getch();
    49. return 0;
    50. }
    51. #endif

char *strstr(const char *str1, const char *str2);