Count occurrences of a word in string
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 occurrences 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 is 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 :
Last Updated on October 28, 2021 by admin