C语言中strstr函数

头文件:#include <string.h>

strstr()函数用来检索子串在字符串中首次出现的位置,其原型为:
    char *strstr( char *str, char * substr );

【参数说明】str为要检索的字符串,substr为要检索的子串。

【返回值】返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL。

【函数示例】strstr()函数的使用。

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main(){
  4. // 也可以改成 char str[] = "http://see.xidian.edu.cn/cpp/u/xitong/";
  5. char *str = "http://see.xidian.edu.cn/cpp/u/xitong/";
  6. char *substr = "see";
  7. char *s = strstr(str, substr);
  8. printf("%s ", s);
  9. return 0;
  10. }

运行结果:
see.xidian.edu.cn/cpp/u/xitong/

意思返回子串首地址,若子串为字符串,将打印字符串

原文地址:https://www.cnblogs.com/the-tops/p/5583647.html