Check if a Word is Present in a Sentence


A sentence is a group of words that are present in a string. In this problem statement, we need to set a specific word and check whether the word is present in a given string or not. In C++, we have some pre-defined functions such as find(), npos(), and istringstream() will be used to Check if a word is present in a sentence.

Using Standard library Function

The program uses a standard library function that allows the programmer to use pre-existing functions of the C compiler such as find() and npos() to perform certain tasks.

Syntax

The following syntax is used in the examples-

find()

The find() is a pre-defined function in C++ that helps to search the elements within a given input.

npos()

The npos() is a static member constant that shows the highest value for an element of type size_t. This simply represents when no matches are found in the substring within the string.

Algorithm

The following steps are-

Step 1: We will start the program by mentioning the required header file such as iostream and string.

Step 2: Then use the recursive function that accept two parameters- s and w of type const string& to fetch the value from given string variables- sentence and word.

Step 3: Then create the variable found of type size_t that is used for standard library function and store the value as built-in function find() that searches the specific word present in the given sentence.

Step 4: Next, use the if-statement to set the condition by using npos and != operator to check whether the given word is present or not. If the word found it returns true otherwise false.

Step 5: Now start the main function of the program and create two string variables- sentence and word that will be used for the operation of the program.

Step 6: Moving ahead to use the calling function in the condition of if-statement to check whether the word is available in the given sentence or not and return the result.

Example

In the following example, we will use two variables to set the values of sentence and word respectively. Then it will use some conditions and operations to check whether the word present in a sentence.

#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;
}

Output

 The given word is present in the sentence.

Using istringstream object

The program uses an istringstream object to check whether the given word present in a sentence or not.

Syntax

The following syntax is used in the examples-

istringstream

The istringstream is a pre-existing object that operates the input stream class into strings. This class contains a sequence of characters.

Algorithm

The following steps are-

Step 1: Start the program by mentioning some of the header files such as iostream, string, and sstream. The header file sstream enables the inner operation of the string object.

Step 2: Then the program uses a recursive function str_word_checker() that accepts two parameters- sentence and word of type const string& which receive the value from the given input string variables.

Step 3: Then it will use the pre-existing object named istringstream check_sentence() that accept the parameter as a sentence to enable the inner operation.

Step 4: Next, initialize the variable t of type string which will be used in the while loop. Using this loop set the condition by mentioning the right shift operator i.e. >> which iterates each word in the sentence by spacing.

Step 5: Inside the while loop, it set the condition of equivalency in if-statement to whether whether the given word is found in the sentence or not. If a word is found it returns true otherwise false.

Step 6: Now start the main function to set the input of two string variables such as s and w to pass the value through the function call.

Step 7: Using an if-else statement, set the condition as a calling function named str_word_checker() to check whether the given word is available in a sentence.

Example

In the following example, the program uses a recursive function that calls itself when required. Then using a simple while loop it set the condition and operation based on word present in a sentence and generates the output.

#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;
}

Output

 The given word is not present in the string.

Conclusion

This program specifies the sentence as a string and the word set to a specific variable which determines whether its present in the given string or not. There were used two methods to solve the problem statement in which we explored the various built-in functions such as find(), npos(), and, istringstream(). There are few applications related to this program such as chatbot filter, word search game, text analysis, etc.

Updated on: 17-Aug-2023

484 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements