strstr函数作用
时间: 2023-10-28 10:24:38 浏览: 102
strstr函数的作用是在一个字符串中查找另一个字符串的出现位置。它的功能是从字符串str1中查找是否有字符串str2,如果有,则返回指向第一次出现的位置的指针。如果没有找到,则返回NULL。
例如,下面是一个使用strstr函数的简单示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello world";
char str2[] = "world";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("'%s' was found in '%s'\n", str2, str1);
printf("'%s' starts at index %ld\n", result, result - str1);
} else {
printf("'%s' was not found in '%s'\n", str2, str1);
}
return 0;
}
```
这段代码会在字符串"hello world"中查找"world",如果找到了,则会输出"'world' was found in 'hello world'"并显示出"world"的起始索引位置。如果没有找到,则会输出"'world' was not found in 'hello world'"。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C++中strstr函数的实现方法总结](https://download.csdn.net/download/weixin_38746387/13780037)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [C语言字符串函数strstr的详细解释](https://blog.csdn.net/nnnggglo/article/details/129915899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文