C++中strrchr的使用示例有哪些
导读:找到字符串中最后一个特定字符的位置: #include <iostream> #include <cstring> int main( { const char* str = "Hello, world...
- 找到字符串中最后一个特定字符的位置:
#include <
iostream>
#include <
cstring>
int main() {
const char* str = "Hello, world!";
char ch = 'o';
const char* lastOccurrence = strrchr(str, ch);
if (lastOccurrence != NULL) {
std::cout <
<
"Last occurrence of '" <
<
ch <
<
"' is at position " <
<
lastOccurrence - str <
<
std::endl;
}
else {
std::cout <
<
"Character '" <
<
ch <
<
"' not found in the string." <
<
std::endl;
}
return 0;
}
- 找到文件路径中文件名的位置:
#include <
iostream>
#include <
cstring>
int main() {
const char* filePath = "C:\\Users\\user\\Documents\\file.txt";
const char* fileName = strrchr(filePath, '\\');
if (fileName != NULL) {
std::cout <
<
"File name is: " <
<
fileName + 1 <
<
std::endl;
// +1 to exclude the backslash
}
else {
std::cout <
<
"File name not found in the path." <
<
std::endl;
}
return 0;
}
这些示例展示了如何在C++中使用strrchr函数来查找字符串中最后一个特定字符的位置。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: C++中strrchr的使用示例有哪些
本文地址: https://pptw.com/jishu/694139.html
