首页主机资讯C++中strchr函数的正确用法

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

时间2024-07-10 16:24:06发布访客分类主机资讯浏览1046
导读:#include <iostream> #include <cstring> int main( { const char str[] = "Hello, World!"; const char...

#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函数来查找指定字符在字符串中的位置。如果找到了指定字符,则打印出该字符的位置;如果没有找到,则打印出未找到的提示信息。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: C++中strchr函数的正确用法
本文地址: https://pptw.com/jishu/687555.html
服务器编程有什么用处呢 编程用什么服务器比较好

游客 回复需填写必要信息