#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函數來查找字符串中最后一個特定字符的位置。