檢查句子中是否存在某個單詞


句子是由字串中存在的單片語成的。在這個問題陳述中,我們需要設定一個特定的單詞,並檢查該單詞是否在一個給定的字串中。在 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.