Check if a word is present in a sentence
Given a sentence as a string str and a word word, the task is to check if the word is present in str or not. A sentence is a string comprised of multiple words and each word is separated with spaces.
Examples:
Input: str = “Beginner for Beginner”, word = “Beginner”
Output: Word is present in the sentenceInput: str = “Beginner for Beginner”, word = “eeks”
Output: Word is not present in the sentence
Approach: In this algorithm, stringstream is used to break the sentence into words then compare each individual word of the sentence with the given word. If the word is found then the function returns true.
Note that this implementation does not search for a sub-sequence or sub-string, it only searches for a complete single word in a sentence.
Below is the implementation for the case-sensitive search approach:
CPP
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if the word is found bool isWordPresent(string sentence, string word) { // To break the sentence in words stringstream s(sentence); // To temporarily store each individual word string temp; while (s >> temp) { // Comparing the current word // with the word to be searched if (temp.compare(word) == 0) { return true ; } } return false ; } // Driver code int main() { string s = "Beginner for Beginner" ; string word = "Beginner" ; if (isWordPresent(s, word)) cout << "Yes" ; else cout << "No" ; return 0; } |
Java
// Java implementation of the approach class GFG { // Function that returns true if the word is found static boolean isWordPresent(String sentence, String word) { // To break the sentence in words String []s = sentence.split( " " ); // To temporarily store each individual word for ( String temp :s) { // Comparing the current word // with the word to be searched if (temp.compareTo(word) == 0 ) { return true ; } } return false ; } // Driver code public static void main(String[] args) { String s = "Beginner for Beginner" ; String word = "Beginner" ; if (isWordPresent(s, word)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by PrinciRaj1992 |
Python
# Python3 implementation of the approach # Function that returns true if the word is found def isWordPresent(sentence, word): # To break the sentence in words s = sentence.split( " " ) for i in s: # Comparing the current word # with the word to be searched if (i = = word): return True return False # Driver code s = "Beginner for Beginner" word = "Beginner" if (isWordPresent(s, word)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if the word is found static bool isWordPresent(String sentence, String word) { // To break the sentence in words String []s = sentence.Split( ' ' ); // To temporarily store each individual word foreach (String temp in s) { // Comparing the current word // with the word to be searched if (temp.CompareTo(word) == 0) { return true ; } } return false ; } // Driver code public static void Main(String[] args) { String s = "Beginner for Beginner" ; String word = "Beginner" ; if (isWordPresent(s, word)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // JavaScript implementation of the approach // Function that returns true if the word is found function isWordPresent(sentence, word) { // To break the sentence in words let s = sentence.split( " " ); // To temporarily store each individual word for ( let temp=0;temp<s.length;temp++) { // Comparing the current word // with the word to be searched if (s[temp] == (word) ) { return true ; } } return false ; } // Driver code let s = "Beginner for Beginner" ; let word = "Beginner" ; if (isWordPresent(s, word)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by patel2127 </script> |
Yes
Time complexity: O(n) where n is the length of the sentence.
Auxiliary space: O(n) where n is the length of string.
Below is the implementation for the case-insensitive search approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if the word is found bool isWordPresent(string sentence, string word) { // To convert the word in uppercase transform(word.begin(), word.end(), word.begin(), :: toupper ); // To convert the complete sentence in uppercase transform(sentence.begin(), sentence.end(), sentence.begin(), :: toupper ); // Both strings are converted to the same case, // so that the search is not case-sensitive // To break the sentence in words stringstream s(sentence); // To store the individual words of the sentence string temp; while (s >> temp) { // Compare the current word // with the word to be searched if (temp.compare(word) == 0) { return true ; } } return false ; } // Driver code int main() { string s = "Beginner for Beginner" ; string word = "Beginner" ; if (isWordPresent(s, word)) cout << "Yes" ; else cout << "No" ; return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if the word is found static boolean isWordPresent(String sentence, String word) { // To convert the word in uppercase word = transform(word); // To convert the complete sentence in uppercase sentence = transform(sentence); // Both Strings are converted to the same case, // so that the search is not case-sensitive // To break the sentence in words String []s = sentence.split( " " ); // To store the individual words of the sentence for ( String temp :s) { // Comparing the current word // with the word to be searched if (temp.compareTo(word) == 0 ) { return true ; } } return false ; } static String transform(String word) { return word.toUpperCase(); } // Driver code public static void main(String[] args) { String s = "Beginner for Beginner" ; String word = "Beginner" ; if (isWordPresent(s, word)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approach # Function that returns true if the word is found def isWordPresent(sentence, word) : # To convert the word in uppercase word = word.upper() # To convert the complete sentence in uppercase sentence = sentence.upper() # Both strings are converted to the same case, # so that the search is not case-sensitive # To break the sentence in words s = sentence.split(); for temp in s : # Compare the current word # with the word to be searched if (temp = = word) : return True ; return False ; # Driver code if __name__ = = "__main__" : s = "Beginner for Beginner" ; word = "Beginner" ; if (isWordPresent(s, word)) : print ( "Yes" ); else : print ( "No" ); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if the word is found static bool isWordPresent(String sentence, String word) { // To convert the word in uppercase word = transform(word); // To convert the complete sentence in uppercase sentence = transform(sentence); // Both Strings are converted to the same case, // so that the search is not case-sensitive // To break the sentence in words String []s = sentence.Split( ' ' ); // To store the individual words of the sentence foreach ( String temp in s) { // Comparing the current word // with the word to be searched if (temp.CompareTo(word) == 0) { return true ; } } return false ; } static String transform(String word) { return word.ToUpper(); } // Driver code public static void Main(String[] args) { String s = "Beginner for Beginner" ; String word = "Beginner" ; if (isWordPresent(s, word)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript implementation of the approach // Function that returns true if the word is found function isWordPresent(sentence,word) { // To convert the word in uppercase word = transform(word); // To convert the complete sentence in uppercase sentence = transform(sentence); // Both Strings are converted to the same case, // so that the search is not case-sensitive // To break the sentence in words let s = sentence.split( " " ); // To store the individual words of the sentence for ( let temp=0;temp<s.length;temp++) { // Comparing the current word // with the word to be searched if (s[temp] == (word)) { return true ; } } return false ; } function transform(word) { return word.toUpperCase(); } // Driver code let s = "Beginner for Beginner" ; let word = "Beginner" ; if (isWordPresent(s, word)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by unknown2108 </script> |
Yes
Time complexity: O(length(s))
Auxiliary space: O(1)
Method #3:Using Built-in Python Functions:
- As all the words in a sentence are separated by spaces.
- We have to split the sentence by spaces using split().
- We split all the words by spaces and store them in a list.
- We use count() function to check whether the word is in array
- If the value of count is greater than 0 then word is present in string
Below is the implementation:
C++
#include <iostream> #include <algorithm> #include <vector> #include <sstream> using namespace std; bool isWordPresent(string sentence, string word) { // Convert the word to uppercase transform(word.begin(), word.end(), word.begin(), :: toupper ); // Convert the sentence to uppercase transform(sentence.begin(), sentence.end(), sentence.begin(), :: toupper ); // Split the sentence into words stringstream ss(sentence); vector<string> words; string w; while (ss >> w) words.push_back(w); // Check if the word is present for ( const auto & w : words) { if (w == word) { return true ; } } return false ; } int main() { string s = "Beginner for Beginner" ; string word = "Beginner" ; if (isWordPresent(s, word)) cout << "Yes\n" ; else cout << "No\n" ; return 0; } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function that returns true // if the word is found static boolean isWordPresent(String sentence, String word) { // To convert the word in uppercase word = word.toUpperCase(); // To convert the complete // sentence in uppercase sentence = sentence.toUpperCase(); // Splitting the sentence to an array String[] words = sentence.split( " " ); // Checking if word is present for (String w : words) { if (w.equals(word)) { return true ; } } return false ; } public static void main(String[] args) { String s = "Beginner for Beginner" ; String word = "Beginner" ; if (isWordPresent(s, word)) System.out.println( "Yes" ); else System.out.println( "No" ); } } // This code is contributed by karthik |
Python3
# Python3 implementation of the approach # Function that returns true # if the word is found def isWordPresent(sentence, word): # To convert the word in uppercase word = word.upper() # To convert the complete # sentence in uppercase sentence = sentence.upper() # splitting the sentence to list lis = sentence.split() # checking if word is present if (lis.count(word) > 0 ): return True else : return False # Driver code s = "Beginner for Beginner" word = "Beginner" if (isWordPresent(s, word)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by vikkycirus |
C#
// C# implementation of the approach using System; public class GFG { // Function that returns true if the word is found static bool isWordPresent( string sentence, string word) { // To convert the word in uppercase word = word.ToUpper(); // To convert the complete sentence in uppercase sentence = sentence.ToUpper(); // Splitting the sentence to an array string [] words = sentence.Split( ' ' ); // Checking if word is present foreach ( string w in words) { if (w.Equals(word)) { return true ; } } return false ; } static public void Main() { // Code string s = "Beginner for Beginner" ; string word = "Beginner" ; if (isWordPresent(s, word)) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed by sankar |
Javascript
<script> // JavaScript implementation of the approach // Function that returns true // if the word is found function isWordPresent(sentence, word){ // To convert the word in uppercase word = word.toUpperCase() // To convert the complete // sentence in uppercase sentence = sentence.toUpperCase() // splitting the sentence to list let lis = sentence.split( ' ' ) // checking if word is present if (lis.indexOf(word) != -1) return true else return false } // Driver code let s = "Beginner for Beginner" let word = "Beginner" if (isWordPresent(s, word)) document.write( "Yes" , "</br>" ) else document.write( "No" , "</br>" ) // This code is contributed by shinjanpatra </script> |
Output:
Yes
Time complexity: O(length(s))
Auxiliary space: O(1)