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 = “Geeks for Geeks”, word = “Geeks”
Output: Word is present in the sentence
Input: str = “Geeks for Geeks”, 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
#include <bits/stdc++.h>
using namespace std;
bool isWordPresent(string sentence, string word)
{
stringstream s(sentence);
string temp;
while (s >> temp) {
if (temp.compare(word) == 0) {
return true ;
}
}
return false ;
}
int main()
{
string s = "Geeks for Geeks" ;
string word = "Geeks" ;
if (isWordPresent(s, word))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isWordPresent(String sentence, String word)
{
String []s = sentence.split( " " );
for ( String temp :s)
{
if (temp.compareTo(word) == 0 )
{
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
String s = "Geeks for Geeks" ;
String word = "Geeks" ;
if (isWordPresent(s, word))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python
def isWordPresent(sentence, word):
s = sentence.split( " " )
for i in s:
if (i = = word):
return True
return False
s = "Geeks for Geeks"
word = "Geeks"
if (isWordPresent(s, word)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isWordPresent(String sentence, String word)
{
String []s = sentence.Split( ' ' );
foreach (String temp in s)
{
if (temp.CompareTo(word) == 0)
{
return true ;
}
}
return false ;
}
public static void Main(String[] args)
{
String s = "Geeks for Geeks" ;
String word = "Geeks" ;
if (isWordPresent(s, word))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isWordPresent(sentence, word)
{
let s = sentence.split( " " );
for ( let temp=0;temp<s.length;temp++)
{
if (s[temp] == (word) )
{
return true ;
}
}
return false ;
}
let s = "Geeks for Geeks" ;
let word = "Geeks" ;
if (isWordPresent(s, word))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
bool isWordPresent(string sentence, string word)
{
transform(word.begin(),
word.end(), word.begin(), :: toupper );
transform(sentence.begin(), sentence.end(),
sentence.begin(), :: toupper );
stringstream s(sentence);
string temp;
while (s >> temp) {
if (temp.compare(word) == 0) {
return true ;
}
}
return false ;
}
int main()
{
string s = "Geeks for Geeks" ;
string word = "geeks" ;
if (isWordPresent(s, word))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isWordPresent(String sentence,
String word)
{
word = transform(word);
sentence = transform(sentence);
String []s = sentence.split( " " );
for ( String temp :s)
{
if (temp.compareTo(word) == 0 )
{
return true ;
}
}
return false ;
}
static String transform(String word)
{
return word.toUpperCase();
}
public static void main(String[] args)
{
String s = "Geeks for Geeks" ;
String word = "geeks" ;
if (isWordPresent(s, word))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isWordPresent(sentence, word) :
word = word.upper()
sentence = sentence.upper()
s = sentence.split();
for temp in s :
if (temp = = word) :
return True ;
return False ;
if __name__ = = "__main__" :
s = "Geeks for Geeks" ;
word = "geeks" ;
if (isWordPresent(s, word)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool isWordPresent(String sentence,
String word)
{
word = transform(word);
sentence = transform(sentence);
String []s = sentence.Split( ' ' );
foreach ( String temp in s)
{
if (temp.CompareTo(word) == 0)
{
return true ;
}
}
return false ;
}
static String transform(String word)
{
return word.ToUpper();
}
public static void Main(String[] args)
{
String s = "Geeks for Geeks" ;
String word = "geeks" ;
if (isWordPresent(s, word))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isWordPresent(sentence,word)
{
word = transform(word);
sentence = transform(sentence);
let s = sentence.split( " " );
for ( let temp=0;temp<s.length;temp++)
{
if (s[temp] == (word))
{
return true ;
}
}
return false ;
}
function transform(word)
{
return word.toUpperCase();
}
let s = "Geeks for Geeks" ;
let word = "geeks" ;
if (isWordPresent(s, word))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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)
{
transform(word.begin(), word.end(), word.begin(), :: toupper );
transform(sentence.begin(), sentence.end(), sentence.begin(), :: toupper );
stringstream ss(sentence);
vector<string> words;
string w;
while (ss >> w)
words.push_back(w);
for ( const auto & w : words) {
if (w == word) {
return true ;
}
}
return false ;
}
int main()
{
string s = "Geeks for Geeks" ;
string word = "geeks" ;
if (isWordPresent(s, word))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isWordPresent(String sentence,
String word)
{
word = word.toUpperCase();
sentence = sentence.toUpperCase();
String[] words = sentence.split( " " );
for (String w : words) {
if (w.equals(word)) {
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
String s = "Geeks for Geeks" ;
String word = "geeks" ;
if (isWordPresent(s, word))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isWordPresent(sentence, word):
word = word.upper()
sentence = sentence.upper()
lis = sentence.split()
if (lis.count(word) > 0 ):
return True
else :
return False
s = "Geeks for Geeks"
word = "geeks"
if (isWordPresent(s, word)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static bool isWordPresent( string sentence, string word)
{
word = word.ToUpper();
sentence = sentence.ToUpper();
string [] words = sentence.Split( ' ' );
foreach ( string w in words)
{
if (w.Equals(word)) {
return true ;
}
}
return false ;
}
static public void Main()
{
string s = "Geeks for Geeks" ;
string word = "geeks" ;
if (isWordPresent(s, word))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isWordPresent(sentence, word){
word = word.toUpperCase()
sentence = sentence.toUpperCase()
let lis = sentence.split( ' ' )
if (lis.indexOf(word) != -1)
return true
else
return false
}
let s = "Geeks for Geeks"
let word = "geeks"
if (isWordPresent(s, word))
document.write( "Yes" , "</br>" )
else
document.write( "No" , "</br>" )
</script>
|
Output:
Yes
Time complexity: O(length(s))
Auxiliary space: O(1)
Last Updated :
06 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...