c语言strstr和strrchr
时间: 2023-11-16 19:00:24 浏览: 57
函数有什么区别?
strchr()和strstr()都是C语言中的字符串处理函数,它们的区别在于查找的对象不同。strchr()函数用于在一个字符串中查找给定字符的第一个匹配,而strstr()函数用于在字符串中查找指定字符串的第一次出现。具体来说,strchr()是查找字符,而strstr()查找字符串。另外,strrchr()函数与strchr()函数类似,但是它是从字符串的末尾开始查找字符。
下面是一个示例代码,演示了strchr()和strstr()函数的用法:
```c
#include <stdio.h>
#include <string.h>
int main() {
const char str[] = "Hello world!";
const char ch = 'w';
const char sub[10] = "wor";
char *ret;
// 使用strchr()函数查找字符
ret = strchr(str, ch);
printf("strchr() %c is %s\n", ch, ret);
// 使用strstr()函数查找字符串
ret = strstr(str, sub);
printf("strstr() %s is %s\n", sub, ret);
return 0;
}
```
相关问题
c语言字符串数组查找
在C语言中,可以使用以下函数来查找字符串数组中的元素:
1. strcmp()函数:用于比较两个字符串是否相等,如果相等则返回0,否则返回非0值。
2. strstr()函数:用于在一个字符串中查找另一个字符串,如果找到则返回第一次出现的位置,否则返回NULL。
3. strchr()函数:用于在一个字符串中查找指定字符,如果找到则返回第一次出现的位置,否则返回NULL。
4. strrchr()函数:用于在一个字符串中查找指定字符,如果找到则返回最后一次出现的位置,否则返回NULL。
以下是一个示例代码,演示如何使用strchr()函数在字符串数组中查找指定字符并打印结果:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[5][20] = {"apple", "banana", "orange", "grape", "pear"};
char c = 'a';
char *ppos;
for (int i = 0; i < 5; i++) {
ppos = strchr(str[i], c);
if (ppos != NULL) {
printf("Found '%c' in '%s' at position %ld\n", c, str[i], ppos - str[i]);
}
}
return 0;
}
```
输出结果为:
```
Found 'a' in 'apple' at position 0
Found 'a' in 'banana' at position 1
Found 'a' in 'grape' at position 3
```
c语言输入字符串库函数
C语言中常用的字符串库函数有很多,以下是其中几个常见的:
1. strlen:用于计算字符串的长度,即不包括终止符'\0'的字符个数。
2. strcpy:用于将源字符串复制到目标字符串中,包括终止符'\0'。
3. strncpy:与strcpy类似,但可以指定复制的最大长度,避免内存溢出。
4. strcat:用于将源字符串连接到目标字符串的末尾,包括终止符'\0'。
5. strncat:与strcat类似,但可以指定连接的最大长度,避免内存溢出。
6. strcmp:用于比较两个字符串的大小,返回值为0表示相等,小于0表示第一个字符串小于第二个字符串,大于0表示第一个字符串大于第二个字符串。
7. strncmp:与strcmp类似,但可以指定比较的最大长度。
8. strchr:用于在字符串中查找某个字符的首次出现位置,并返回该位置的指针。
9. strrchr:与strchr类似,但查找的是字符在字符串中最后一次出现的位置。
10. strstr:用于在字符串中查找某个子串的首次出现位置,并返回该位置的指针。
11. strtok:用于将字符串按照指定的分隔符进行分割,并返回分割后的子串。
这些函数可以在C语言的string.h头文件中找到对应的函数声明。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C语言常用字符串库函数总结](https://blog.csdn.net/weixin_44098461/article/details/115472138)[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语言字符与字符串的库函数](https://blog.csdn.net/qq_61213622/article/details/125884482)[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 ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)