📜  检查句子中是否存在单词

📅  最后修改于: 2022-05-13 01:57:06.910000             🧑  作者: Mango

检查句子中是否存在单词

给定一个句子作为字符串str和一个单词word ,任务是检查该单词是否存在于str中。句子是由多个单词组成的字符串,每个单词用空格分隔。
例子:

方法:在该算法中,字符串流用于将句子分解为单词,然后将句子中的每个单词与给定单词进行比较。如果找到该单词,则该函数返回 true。
请注意,此实现不搜索子序列或子字符串,它仅搜索句子中的完整单个单词。
以下是区分大小写的搜索方法的实现:

CPP
// C++ implementation of the approach
#include 
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 = "Geeks for Geeks";
    string word = "Geeks";
 
    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 = "Geeks for Geeks";
    String word = "Geeks";
 
    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 = "Geeks for Geeks"
word = "Geeks"
 
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 = "Geeks for Geeks";
    String word = "Geeks";
 
    if (isWordPresent(s, word))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ implementation of the approach
#include 
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 = "Geeks for Geeks";
    string word = "geeks";
 
    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 = "Geeks for Geeks";
    String word = "geeks";
 
    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 = "Geeks for Geeks";
    word = "geeks";
 
    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 = "Geeks for Geeks";
    String word = "geeks";
 
    if (isWordPresent(s, word))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


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 = "Geeks for Geeks"
word = "geeks"
if (isWordPresent(s, word)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by vikkycirus


输出:
Yes

以下是不区分大小写搜索方法的实现:

C++

// C++ implementation of the approach
#include 
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 = "Geeks for Geeks";
    string word = "geeks";
 
    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 = "Geeks for Geeks";
    String word = "geeks";
 
    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 = "Geeks for Geeks";
    word = "geeks";
 
    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 = "Geeks for Geeks";
    String word = "geeks";
 
    if (isWordPresent(s, word))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by 29AjayKumar

Javascript


输出:
Yes

方法#3:使用内置Python函数:

  • 因为句子中的所有单词都用空格分隔。
  • 我们必须使用 split() 将句子按空格分隔。
  • 我们用空格分割所有单词并将它们存储在一个列表中。
  • 我们使用count()函数来检查单词是否在数组中
  • 如果 count 的值大于 0 则 word 存在于字符串中

下面是实现:

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 = "Geeks for Geeks"
word = "geeks"
if (isWordPresent(s, word)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by vikkycirus

输出:

Yes