温馨提示×

C++中strchr函数的正确用法

c++
小樊
86
2024-07-10 12:01:29
栏目: 编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

#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函数的正确用法

0