Python 检查一个单词是否在一个句子中出现
一个句子是由一组单词组成的字符串。在这个问题的陈述中,我们需要设定一个特定的单词,并检查给定的字符串中是否存在该单词。在C++中,我们可以使用一些预定义的函数,如find()、npos()和istringstream()来检查一个句子中是否出现一个单词。
使用标准库函数
该程序使用了一个标准库函数,使程序员能够使用C编译器的预定义函数,如find()和npos(),来执行特定的任务。
语法
以下是示例中使用的语法-
find()
find() 是 C++ 中的预定义函数,用于在给定的输入中寻找元素。
npos()
npos()是一个静态成员常量,表示类型为size_t的元素的最大值。它表示在字符串中的子字符串中没有找到匹配项的情况。
步骤
以下是步骤:
步骤1:我们将通过指定所需的头文件,如iostream和string,来开始程序。
步骤2:然后使用递归函数,接受两个参数-s和w,类型为const string&,从给定的字符串变量sentence和word中获取值。
步骤3:然后创建一个名为found的变量,类型为size_t,用于标准库函数,并将值存储为内置函数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(),该函数接受两个参数-以const string&类型为sentence和word,从给定的输入字符串变量中接收值。
步骤3:然后它将使用一个名为check_sentence()的现有对象istringstream,该对象接受sentence作为参数以进行内部操作。
步骤4:接下来,初始化一个名为t的类型为string的变量,该变量将在while循环中使用。使用这个循环设置条件,通过指定右移运算符即“>>”,该运算符按空格迭代句子中的每个单词。
步骤5:在while循环内部,设置了等价条件的if语句,判断给定单词是否在句子中找到。如果找到了一个单词,则返回true,否则返回false。
步骤6:现在开始主函数,设置两个string变量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()。该程序与一些相关应用有关,例如聊天机器人过滤器、单词搜索游戏、文本分析等。