strstr函数用于在一个字符串中查找另一个字符串第一次出现的位置
strstr函数用于在一个字符串中查找另一个字符串第一次出现的位置。函数原型如下:
char*strstr(constchar*haystack,constchar*needle);
其中,haystack
表示要查找的字符串,needle
表示要搜索的子字符串。
使用方法如下:
#include<stdio.h>
#include<string.h>
intmain(){
constchar*haystack="Hello,world!";
constchar*needle="world";
char*result=strstr(haystack,needle);
if(result){
printf("'%s'foundatposition%ld\n",needle,result-haystack);
}else{
printf("'%s'notfound\n",needle);
}
return0;
}
以上代码将在haystack
中查找needle
,如果找到则输出其位置,否则输出未找到的信息。
版权声明
本文仅代表作者观点,不代表博信信息网立场。