【c&c++】C语言:strstr()函数用来检索子串在字符串中首次出现的位置
一、strstr()函数用来检索子串在字符串中首次出现的位置,其原型为: char *strstr( char *str, char * substr );
1、头文件:#include <string.h>
2、参数说明:str为要检索的字符串,substr为要检索的子串。
3、返回值:返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL。
二、实例测试
1、测试c源码
#include<stdio.h> #include<string.h> int main(){ char *str = "https://www.sina.com.cn/"; char *substr = "www"; char *substr2 = "adb"; char *s = strstr(str, substr); if(s==NULL) printf("can't find %s in %s\n",substr,str); else printf("%s include %s;show the string from start found address:%s\n", str,substr,s); char *s2 = strstr(str, substr2); if(s2==NULL) printf("can't find %s in %s\n",substr2,str); else printf("%s\n", s2); return 0; }
2、测试结果

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2020-02-10 【mysql】GPS应用