在C語言中,可以使用strstr()函數在一個字符串中查找另一個字符串
#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;
}
在這個示例中,我們定義了兩個字符串變量haystack和needle。然后,我們使用strstr()函數在haystack中查找needle。如果找到了匹配的子字符串,strstr()函數將返回一個指向該子字符串的指針,否則返回NULL。我們使用if語句檢查結果,并根據是否找到子字符串打印相應的消息。