You are given a string and a word your task is that count the number of the occurrence of the given word in the string and print the number of occurrence of the word.
Examples:
Input : string = "GeeksforGeeks A computer science portal for geeks" word = "portal" Output : Occurrences of Word = 1 Time Input : string = "GeeksforGeeks A computer science portal for geeks" word = "technical" Output : Occurrences of Word = 0 Time
Approach :-
- First, we split the string by spaces in a
- Then, take a variable count = 0 and in every true condition we increment the count by 1
- Now run a loop at 0 to length of string and check if our string is equal to the word
- if condition true then we increment the value of count by 1 and in the end we print the value of count.
Below is the implementation of the above approach :
C++
// C++ program to count the number // of occurrence of a word in // the given string given string #include <bits/stdc++.h> using namespace std; int countOccurences(char *str, string word) { char *p; // split the string by spaces in a vector<string> a; p = strtok(str, " "); while (p != NULL) { a.push_back(p); p = strtok(NULL, " "); } // search for pattern in a int c = 0; for (int i = 0; i < a.size(); i++) // if match found increase count if (word == a[i]) c++; return c; } // Driver code int main() { char str[] = "GeeksforGeeks A computer science portal for geeks "; string word = "portal"; cout << countOccurences(str, word); return 0; } // This code is contributed by // sanjeev2552 |
Java
// Java program to count the number // of occurrence of a word in // the given string given string import java.io.*; class GFG { static int countOccurences(String str, String word) { // split the string by spaces in a String a[] = str.split(" "); // search for pattern in a int count = 0; for (int i = 0; i < a.length; i++) { // if match found increase count if (word.equals(a[i])) count++; } return count; } // Driver code public static void main(String args[]) { String str = "GeeksforGeeks A computer science portal for geeks "; String word = "portal"; System.out.println(countOccurences(str, word)); } } /*This code is contributed by Nikita Tiwari.*/ |
Python 3
# Python program to count the number of occurrence # of a word in the given string given string def countOccurences(str, word): # split the string by spaces in a a = str.split(" ") # search for pattern in a count = 0 for i in range(0, len(a)): # if match found increase count if (word == a[i]): count = count + 1 return count # Driver code str ="GeeksforGeeks A computer science portal for geeks "word ="portal"print(countOccurences(str, word)) |
C#
// C# program to count the number // of occurrence of a word in // the given string given string using System; class GFG { static int countOccurences(string str, string word) { // split the string by spaces string[] a = str.Split(' '); // search for pattern in string int count = 0; for (int i = 0; i < a.Length; i++) { // if match found increase count if (word.Equals(a[i])) count++; } return count; } // Driver code public static void Main() { string str = "GeeksforGeeks A computer science portal for geeks "; string word = "portal"; Console.Write(countOccurences(str, word)); } } // This code is contributed // by ChitraNayal |
PHP
<?php // PHP program to count the number // of occurrence of a word in // the given string given string function countOccurences($str, $word) { // split the string by spaces $a = explode(" ", $str); // search for pattern in string $count = 0; for ($i = 0; $i < sizeof($a); $i++) { // if match found increase count if ($word == $a[$i]) $count++; } return $count; } // Driver code $str = "GeeksforGeeks A computer science portal for geeks "; $word = "portal"; echo (countOccurences($str, $word)); // This code is contributed // by ChitraNayal ?> |
Output:
1
Reference : split function python
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Python | Count occurrences of each word in given text file (Using dictionary)
- Count occurrences of a string that can be constructed from another given string
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- Python program to read file word by word
- C program to find and replace a word in a File by another given word
- Count of occurrences of a "1(0+)1" pattern in a string
- Count occurrences of a character in a repeated string
- Python | Count occurrences of a character in string
- Count occurrences of a sub-string with one variable character
- Python | Count String occurrences in mixed list
- Count of occurrences of each prefix in a string using modified KMP algorithm
- String containing first letter of every word in a given string with spaces
- Most frequent word in first String which is not present in second String
- Python | Count occurrences of an element in a list
- Python | Count occurrences of an element in a Tuple
- Count occurrences of a substring recursively
- Count Occurrences of Anagrams
- Python program to Count the Number of occurrences of a key-value pair in a text file
- Replace all occurrences of string AB with C without using extra space
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

