在C語言中,可以使用strstr()
函數(shù)在一個(gè)字符串中查找另一個(gè)字符串
#include<stdio.h>
#include<string.h>
int main() {
char haystack[] = "Hello, welcome to the world of programming!";
char needle[] = "world";
char *result = strstr(haystack, needle);
if (result) {
printf("Found the string '%s' in the text '%s'.\n", needle, haystack);
} else {
printf("The string '%s' was not found in the text '%s'.\n", needle, haystack);
}
return 0;
}
在這個(gè)示例中,我們定義了兩個(gè)字符串變量haystack
和needle
。然后,我們使用strstr()
函數(shù)在haystack
中查找needle
。如果找到了匹配的子字符串,strstr()
函數(shù)將返回一個(gè)指向該子字符串的指針,否則返回NULL
。我們使用if
語句檢查結(jié)果,并根據(jù)是否找到子字符串打印相應(yīng)的消息。