C strstr 函数是字符串函数,它返回指向给定字符串中第一次出现子字符串的指针。strstr 方法的语法是:
void *strstr(const char *str, const char *str_to_look);
- str:一个有效的字符串
- str_to_look:您想在 str 中搜索的文本。
strstr 函数示例
strstr 函数在用户指定的字符串中搜索子字符串的第一次出现。
在使用此函数之前,您必须包含 #include<string.h> 头文件。此程序将帮助您通过使用 if else 语句和多个示例来理解 strstr。在这里,如果找到指向给定子字符串的指针,则会打印 if 块内的语句;否则,将通过编程打印 else 块中的语句。
#include <stdio.h>
#include<string.h>
int main()
{
char str[] = "This is abc working in abc Company";
char *res;
res = strstr(str, "abc");
if(res)
{
printf("We Found your String");
printf("\nThe Final String From 'abc' is : %s \n", res);
}
else
{
printf("String Not found. Sorry!! \n");
}
return 0;
}
