检查句子中是否存在某个单词


句子是由字符串中存在的单词组成的。在这个问题陈述中,我们需要设置一个特定的单词,并检查该单词是否在一个给定的字符串中。在 C++ 中,我们有一些预定义的函数,例如 find()、npos() 和 istringstream(),可用于检查句子中是否存在某个单词。

使用标准库函数

该程序使用标准库函数,允许程序员使用 C 编译器的预先存在的函数(例如 find() 和 npos())来执行某些任务。

语法

以下语法用于示例中:

find()

find() 是 C++ 中一个预定义函数,用于搜索给定输入中的元素。

npos()

npos() 是一个静态成员常量,表示 size_t 类型元素的最大值。这表示在字符串中的子字符串中未找到匹配项。

算法

以下步骤:

步骤 1:我们将从提及所需的标头文件(例如 iostream 和 string)开始程序。

步骤 2:然后使用递归函数,该函数接受两个参数 s 和 w(类型为 const string&),以从给定的字符串变量(句子和单词)中获取值。

步骤 3:然后创建类型为 size_t 的变量 found,该变量用于标准库函数,并将值存储为内置函数 find(),该函数搜索给定句子中存在的特定单词。

步骤 4:接下来,使用 if 语句使用 npos 和 != 运算符设置条件,以检查给定单词是否存在。如果找到该单词,则返回 true,否则返回 false。

步骤 5:现在启动程序的主函数,并创建两个字符串变量 sentence 和 word,它们将用于程序的操作。

步骤 6:继续在 if 语句的条件下使用调用函数,以检查给定单词是否在给定句子中,并返回结果。

示例

在以下示例中,我们将使用两个变量分别设置 sentence 和 word 的值。然后,它将使用一些条件和操作来检查句子中是否存在该单词。

#include <iostream>
#include <string>
using namespace std;
bool word_checker(const string& s, const string& w) {
// Find the index of the word in the sentences
    size_t found = s.find(w);
// Check if the word is found
    if (found != string::npos)
        return true;
  
    return false;
}
// Start the main function
int main() {
    string sentence = "The bestseller book";
    string word = "book";
// Check whether the given word present in the sentence
    if (word_checker(sentence, word))
        cout << "The given word is present in the sentence." << endl;
    else
        cout << "The given word is not present in the sentence." << endl;
    return 0;
}

输出

 The given word is present in the sentence.

使用 istringstream 对象

该程序使用 istringstream 对象来检查给定单词是否存在于句子中。

语法

以下语法用于示例中:

istringstream

istringstream 是一个预先存在的对象,它将输入流类操作到字符串中。此类包含一系列字符。

算法

以下步骤:

步骤 1:通过提及一些头文件(例如 iostream、string 和 sstream)来启动程序。sstream 头文件支持字符串对象的内部操作。

步骤 2:然后,程序使用递归函数 str_word_checker(),该函数接受两个参数 sentence 和 word(类型为 const string&),它们接收来自给定输入字符串变量的值。

步骤 3:然后,它将使用名为 istringstream 的预先存在的对象 check_sentence(),该对象接受句子作为参数以启用内部操作。

步骤 4:接下来,初始化类型为 string 的变量 t,该变量将在 while 循环中使用。使用此循环通过提及右移运算符(即 >>)设置条件,该运算符通过空格迭代句子中的每个单词。

步骤 5:在 while 循环内,它在 if 语句中设置等效条件,以检查给定单词是否在句子中找到。如果找到一个单词,则返回 true,否则返回 false。

步骤 6:现在启动主函数以设置两个字符串变量(例如 s 和 w)的输入,以通过函数调用传递值。

步骤 7:使用 if-else 语句,将条件设置为名为 str_word_checker() 的调用函数,以检查给定单词是否在句子中。

示例

在以下示例中,程序使用一个递归函数,该函数在需要时调用自身。然后,它使用一个简单的 while 循环根据句子中存在的单词设置条件和操作,并生成输出。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool str_word_checker(const string& sentence, const string& word) {
istringstream check_sentence(sentence);
string t; 
// the sentence by space using the >> operator
    while (check_sentence >> t) {
// Check if the specific word matches 
        if (t == word)
            return true;
    }
    return false;
}
// Start the main function
int main() {
string s = "Welcome to C++ Tutorials";
string w = "C+++";
// Check whether the word is present in a given list or not.
    if (str_word_checker(s, w))
        cout << "The given is word present in the string." << std::endl;
    else
        cout << "The given word is not present in the string." << std::endl;
return 0;
}

输出

 The given word is not present in the string.

结论

此程序将句子指定为字符串,并将单词设置为特定变量,该变量确定其是否存在于给定的字符串中。使用了两种方法来解决问题陈述,在这些方法中,我们探索了各种内置函数,例如 find()、npos() 和 istringstream()。与该程序相关的应用程序很少,例如聊天机器人过滤器、单词搜索游戏、文本分析等。

更新于:2023年8月17日

856 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.