Check if given words are present in a string


C++ has a predefined function find() which enables searching from the first element range until the last element. In this article, we are going to understand how we can use this find() function to check if a given word is present in a string or not.

Let’s take an example of this.

The given string is “John and mark have same color of t-shirt”;

For searching the word in a string we will make a variable that is denoted as a search finder. Let’s take two variables and check if the given words are present or not.

Var1 = peter; - This word is not found in a string.

Var2 = mark; − This word is found in a string.

So we will apply the condition based on pattern matches and make them satisfied to find the given word in a string.

In this article, we are going to solve whether the given words are present in a string or not.

Methods

find()

The find() is a predefined method in the string that helps to search the given word is present in a string.

string::npos

The npos means no position which indicates if it returns true that means no matches were discovered at any position.

Algorithm

  • We will start with the header file namely ‘iostream’ and ‘string’.

  • We are starting the main function and initializing the string value to the variable named ‘myString’. We are going to find the given word from the string stored in this variable.

  • We will create two variables namely ‘word1’ and ‘word2’. We are going to search for these two words in the myString variable.

  • Now we are using an if-statement to check whether the given word is present in a string or not.

    ‘myString.find( word1 ) != string::npos’

  • Next, the find() function with ‘myString’ variable helps to search the given word and if it is not equivalent to string::npos then it will return the statement as given word is present in a string. If the word is not found then it will return the statement as given word is not present in a string.

Example

In this program, we are going to solve whether the given words are present in a string or not.

#include <iostream>
#include <string>
using namespace std;
int main() {
   string myString = "The quick grey monkey jumps over the lazy dog";
   string word1 = "grey";
   string word2 = "mouse";
   // check whether the given word present in string or not.
   if ( myString.find( word1 ) != string::npos ) {
      cout << word1 << " is present in the string." << endl;
   } else {
      cout << word1 << " is not present in the string." << endl;
   }
   // second time searching the word but process is same
   if ( myString.find( word2 ) != string::npos ) {
      cout << word2 << " is present in the string." << endl;
   } else {
      cout << word2 << " is not present in the string." << endl;
   }
   return 0;
}

Output

grey is present in the string.
mouse is not present in the string.

Conclusion

We explored the concept of whether a given word in a string is present or not. We saw how find() method helps to search the word of a given string, on the other side when this method is equivalent to string::pos it returns the word present in a string otherwise the word is not present.

Updated on: 10-May-2023

598 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements