是否具有确定在c字符串中是否找到某个字符的功能?

时间:2019-04-13 18:49:57

标签: c++

我想通过使用文件流并逐字检查数据来确定句子的结尾。在该项目中,表示句子结尾的唯一字符是na.approx

我正在寻找period .的函数,以确定某个单词中是否存在句点。

我已经尝试过使用字符串查找功能,但是老实说,即使在联机查找引用之后,也无法弄清楚返回类型是什么以及如何构造该函数。

1 个答案:

答案 0 :(得分:0)

成员函数std::string::find仅将句点.传递给它,如果找到,它将为您返回位置:

std::string sText{ "I've tried using the string find function but honestly cannot figure out what the return type is and how to structure the function, even after looking at references online." };
std::string::size_type n{ sText.find('^') };
if (n != std::string::npos)
    cout << "found at: " << n << endl;
else
    cout << "Not found!" << endl;
  • 如果必须使用常量字符串,则可以使用strchr

    const char *str = "I've tried using the string find function but honestly cannot figure out what the return type is and how to structure the function, even after looking at references online.";
    char target = '.';
    const char *result{ str };
    result = std::strchr(result, target);
    if (result)
        cout << "found at: " << result - str << endl;
    else
        cout << "Not found!" << endl;