在C++中,strchr函数用于在一个字符串中查找第一次出现某个特定字符的位置
#include <iostream>
#include <cstring>
int main() {
    const char* str = ""; // 空字符串
    char target = 'a';
    char* result = strchr(str, target);
    if (result != nullptr) {
        std::cout << "字符 '" << target << "' 在字符串中找到了,位置为: " << result - str << std::endl;
    } else {
        std::cout << "字符 '" << target << "' 在字符串中没有找到。" << std::endl;
    }
    return 0;
}
在这个例子中,我们查找空字符串中是否包含字符’a’。strchr函数返回一个指向找到的字符的指针,如果没有找到则返回nullptr。因此,我们可以使用if语句来检查返回值是否为nullptr,从而确定字符串中是否包含目标字符。