在C语言中,可以使用strstr()函数来查找指定字符串在另一个字符串中的位置。strstr()函数接受两个参数,第一个参数是要查找的字符串,第二个参数是要在其中查找的目标字符串。
例如,下面是一个简单的示例,演示如何在一个字符串中查找另一个字符串:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, world!";
char str2[] = "world";
char *ptr;
ptr = strstr(str1, str2);
if (ptr) {
printf("'%s' found in '%s' at position %ld\n", str2, str1, ptr - str1);
} else {
printf("'%s' not found in '%s'\n", str2, str1);
}
return 0;
}
在上面的示例中,strstr()函数会在str1字符串中查找str2字符串。如果str2字符串在str1中被找到,strstr()函数会返回一个指向第一次出现的位置的指针。如果没有找到,则会返回NULL。