在C语言中,可以使用strstr()函数来在一个字符串中查找另一个字符串。该函数的原型为:
char *strstr(const char *haystack, const char *needle);
其中,haystack是要查找的字符串,needle是要查找的子字符串。该函数返回子字符串在字符串中的第一个匹配位置的指针,如果未找到则返回NULL。
下面是一个示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, world!";
char str2[] = "world";
char *ptr = strstr(str1, str2);
if (ptr) {
printf("'%s' found at position %ld\n", str2, ptr - str1);
} else {
printf("'%s' not found\n", str2);
}
return 0;
}
运行结果为:
'world' found at position 7
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/985003.html