#include <iostream>
#include <cstring>
int main() {
const char str[] = "Hello, World!";
const char ch = 'o';
const char *result = strchr(str, ch);
if (result) {
std::cout << "Character '" << ch << "' found at position " << result - str << std::endl;
} else {
std::cout << "Character '" << ch << "' not found in the string." << std::endl;
}
return 0;
}
这段代码演示了如何正确使用strchr
函数来查找指定字符在字符串中的位置。如果找到了指定字符,则打印出该字符的位置;如果没有找到,则打印出未找到的提示信息。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:C++ strstr函数的正确用法