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.
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
- 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 :
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.
- 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
- Count of occurrences of a "1(0+)1" pattern in a string
- Count occurrences of a sub-string with one variable character
- Count occurrences of a character in a repeated string
- Python | Count occurrences of a character in string
- Python | Count String occurrences in mixed list
- Count of occurrences of each prefix in a string using modified KMP algorithm
- Count Occurrences of Anagrams
- 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
- Count occurrences of a substring recursively
- Replace all occurrences of pi with 3.14 in a given string
- Python | Count occurrences of an element in a list
- Python | Count occurrences of an element in a Tuple
- Python | All occurrences of substring in string
- Remove all occurrences of a character in a string
- Replace all occurrences of a string with space
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- 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 firstname.lastname@example.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.