Skip to content
Related Articles

Related Articles

Count words present in a string

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 13 Jul, 2022
Improve Article
Save Article

Given an array of words and a string, we need to count all words that are present in given string. Examples:

Input : words[] = { "welcome", "to", "geeks", "portal"}
            str = "geeksforgeeks is a computer science portal for geeks."
Output :  2
Two words "portal" and "geeks" is present in str.


Input : words[] = {"Save", "Water", "Save", "Yourself"}
        str     = "Save"
Output :1

Steps:

  1. Extract each word from string.
  2. For each word, check if it is in word array(by creating set/map). If present, increment result.

Implementation:

Java




// Java program to count number
// of words present in a string
 
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test {
    static int countOccurrence(String[] word, String str)
    {
        // counter
        int counter = 0;
 
        // for extracting words
        Pattern p = Pattern.compile("[a-zA-Z]+");
        Matcher m = p.matcher(str);
 
        // HashSet for quick check whether
        // a word in str present in word[] or not
        HashSet<String> hs = new HashSet<String>();
 
        for (String string : word) {
            hs.add(string);
        }
 
        while (m.find()) {
            if (hs.contains(m.group()))
                counter++;
        }
 
        return counter;
    }
 
    public static void main(String[] args)
    {
        String word[]
            = { "welcome", "to", "geeks", "portal" };
 
        String str
            = "geeksforgeeks is a computer science portal for geeks.";
 
        System.out.println(countOccurrence(word, str));
    }
}


Output

2

Method:  First iterate the words using for loop and Check if that word is present in the given string or not if present then increment the count value. Finally print the count value.

Implementation:

Python3




# python code to count words present in a string
w=["welcome", "to", "geeks", "portal"]
s="geeksforgeeks is a computer science portal for geeks."
c=0
#iterating words
for i in w:
  #checking if word is present in the string or not
  if i in s:
    # if present then it increments the count value
    c+=1
# printing the result
print(c)
 
 
# this code is contributed by gangarajula laxmi


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        string[] w = { "welcome", "to", "geeks", "portal" };
        string s
            = "geeksforgeeks is a computer science portal for geeks.";
        int c = 0;
        // iterating words
        for (int i = 0; i < w.Length; i++) {
            // checking if word is present in the string or
            // not
            if (s.Contains(w[i])) {
                // if present then it increments the count
                // value
                c = c + 1;
            }
        }
        // printing the result
        Console.WriteLine(c);
    }
}
// This code is contributed by Potta Lokesh


Output

2

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!