Find the word from a given sentence having given word as prefix


When working with natural language processing or text analysis, it is often necessary to search for specific words or phrases within a larger body of text. One common task is finding all the words in a sentence that start with a given prefix. In this article, we will explore how to accomplish this task using C++.

Algorithm

  • Read in the input sentence and prefix.

  • Tokenize the input sentence into individual words.

  • For each word in the sentence, check if it starts with the given prefix.

  • If the word starts with the prefix, add it to the list of words that match.

  • Print the list of words that match.

Example

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
   string sentence, prefix;
   vector<string> words;
   
   // Read in the input sentence and prefix
   sentence="The quick brown fox jumps over the lazy dog";
   prefix="fox";
   
   // Tokenize the input sentence into individual words
   string word = "";
   for (auto c : sentence) {
      if (c == ' ') {
         words.push_back(word);
         word = "";
      }
      else {
         word += c;
      }
   }
   words.push_back(word);

   // Find all words in the sentence that start with the given prefix
   vector<string> matches;
   for (auto w : words) {
      if (w.substr(0, prefix.length()) == prefix) {
         matches.push_back(w);
      }
   }
   
   // Print the list of matching words
   cout << "Matching words:" << endl;
   for (auto m : matches) {
      cout << m << endl;
   }
   
   return 0;
}

Output

Matching words:
fox

Testcase Example

Suppose we have the following input sentence:

The quick brown fox jumps over the lazy dog

And we want to find all the words that start with the prefix "fox". Running the above code with this input would produce the following output:

In this example, the only word in the sentence that starts with the prefix "fox" is "fox" itself, so it is the only word that is printed as a match.

Conclusion

Finding all the words in a sentence that start with a given prefix is a useful task in natural language processing and text analysis. By tokenizing the input sentence into individual words and checking each word for a matching prefix, we can easily accomplish this task using C++.

Updated on: 17-May-2023

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements