strrchr函数用法
时间: 2024-04-25 11:15:02 浏览: 23
strrchr函数用于在字符串中搜索指定字符的最后一次出现位置,并返回该位置及其后面的所有字符。
函数原型如下:
```c
char *strrchr(const char *str, int c)
```
参数说明:
- `str`:要搜索的字符串
- `c`:要搜索的字符
函数返回值:
- 如果找到指定字符,则返回该字符的地址;如果未找到指定字符,则返回NULL。
示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char *ptr;
ptr = strrchr(str, 'o');
if (ptr != NULL) {
printf("Last occurrence of 'o' found at position: %ld\n", ptr - str);
printf("Remaining string: %s\n", ptr);
} else {
printf("Character not found\n");
}
return 0;
}
```
输出结果:
```
Last occurrence of 'o' found at position: 7
Remaining string: orld
```
相关问题
strrchr函数的用法c++
strrchr函数是C语言中的一个字符串处理函数,用于查找一个字符在另一个字符串中最后出现的位置,并返回该位置的地址。在C++中,可以使用该函数来处理字符串,只需包含<string.h>头文件即可。
使用strrchr函数的步骤如下:
1. 包含<string.h>头文件。
2. 声明一个字符指针变量,用于接收strrchr函数的返回值。
3. 调用strrchr函数,传入要查找的字符串和要查找的字符作为参数。
4. 判断返回值是否为NULL,如果不为NULL,则表示找到了指定字符,可以通过返回的地址来操作字符串。
5. 如果返回值为NULL,则表示未找到指定字符。
下面是一个使用strrchr函数的示例代码:
```cpp
#include <string.h>
#include <iostream>
int main() {
const char* str = "Hello, World!";
char c = 'o';
const char* ptr = strrchr(str, c);
if (ptr) {
std::cout << "The character " << c << " is at position: " << ptr << std::endl;
} else {
std::cout << "The character was not found" << std::endl;
}
return 0;
}
```
在这个示例中,我们定义了一个字符串str和一个字符c,然后使用strrchr函数查找字符c在字符串str中最后出现的位置,并将返回的地址赋值给指针ptr。最后,根据ptr的值输出相应的结果。
希望这个例子能够帮助你理解strrchr函数在C++中的用法。
#### 引用[.reference_title]
- *1* [c/c++ strrchr函数用法](https://blog.csdn.net/pengnanzheng/article/details/78996314)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
strrchr()函数
`strrchr()` 是一个在 C 语言中常用的字符串函数,它用于查找一个字符串中最后一个匹配某个特定字符的索引。如果找到了匹配的字符,它会返回一个指向该字符的指针;如果没有找到,它会返回 NULL。
函数的原型通常如下:
```c
char *strrchr(const char *str, int c);
```
参数说明:
* `str`:这是你要在其中搜索的字符串。
* `c`:这是你要查找的字符。
函数返回一个指向找到的字符的指针,如果没有找到则返回 NULL。
这个函数的使用方法如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
char *last_ptr = strrchr(str, 'l');
if (last_ptr != NULL) {
printf("Last occurrence of 'l' is at position: %ld\n", (long) last_ptr - (long) str);
} else {
printf("Character not found.\n");
}
return 0;
}
```
这个程序会输出 "Last occurrence of 'l' is at position: 7",因为 'l' 在字符串中的最后一个位置是第 7 个字符。
需要注意的是,`strrchr()` 函数在找到匹配的字符后会移动到该字符之后的所有字符,所以在返回的指针中使用的 `-` 操作符不会得到最后一个匹配字符后的下一个字符。如果你想找到最后一个匹配字符后的下一个字符,你应该使用 `strchr()` 或 `memrchr()` 函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)