Given a string , find the number of times the word "the" occurs in the string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
/* Initialize the word "the", substring, the count of which is extracted from the main string s */
char *s,*v="the";
int slen,i,index,j,k,flag,count=0;
/* Allocate memory for the input string */
s=(char*)malloc(sizeof(s)*256);
/* Get the input string from the user */
scanf("%s",s);
/* Calculate the length of the input string (number of characters except '\0'(null character)) */
slen=strlen(s);
/* Until we reach the end of the string */
for(i=0;i<slen;i++)
{
/* Set the flag value to 1 */
flag=1;
/* Compare the first character occurence of the first character from the substring "the" in main string */
if(*(s+i)==*v)
{
/* If the first character of both the string matches */
k=i;
/* Until we reach the end of the sub-string "the" , check if all the characters of it matches with the characters of the main string from that index */
for(j=0;*(v+j);j++,k++)
{
/* If any of the character in the sub-string doesn't match the characters in the main string , set the flag value as 0(zero) */
if(*(s+k)!=*(v+j))
{
flag=0;
break;
}
}
/* If the flag value is not set , then there exists atleast one substring that matches in the main string */
if(flag)
{
/* Increment the count of the sub-string occurence all the characters of sub-string matches a part of the main string */
count++;
}
}
}
/* Print the count of the occurence of the word "the" in the main string */
printf("count of 'the' in %s is %d",s,count);
return 0;
}