- String: Collection of character.
Logic to count occurrences of a word in a string:
- First, we take the string. (full statement)
- We take the word which we want to find count from the given string.
- Take a variable count to store count of word occurrences.
- Then we start searching the occurrences of word in string.
- We start reading the character by character till we encounter the space if space encountered then we consider it as one word and store it to another variable.
- Then we check stored word with our taken word (which we searching in a string).
- If it matches we increment the count else not.
- Again, we continue by taking next word same as above.
- This process continues till we encounter the end of a string.
- In the end, we have a count of the occurrences of word into string.
String: The Technical Seek is the technical site
Word to count: technical
The number of occurrences of the word is = 2
Now we will implement this program in c.
C program to count occurrences of a word in a string