C program to accept a string and find the number of times the word ‘the’ appears in it

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C program to accept a string and find the number of times the word ‘the’ appears in it */

#include<stdio.h>
#include<conio.h>

void main()
{
int count=0,i,times=0,t,h,e,space;
char str[100];

clrscr();
puts(“Enter a string:”);
gets(str);

/*Traverse the string to count the number of characters*/
while (str[count]!=’\0′)
{
count++;
}
/*Finding the frequency of the word ‘the’*/
for(i=0;i<=count-3;i++)
{
t=(str[i]==’t'||str[i]==’T');
h=(str[i+1]==’h'||str[i+1]==’H');
e=(str[i+2]==’e'||str[i+2]==’E');
space=(str[i+3]==’ ‘||str[i+3]==’\0′);
if ((t&&h&&e&&space)==1)
times++;
}
printf(“Frequency of the word \’the\’ is %d\n”,times);
getch();
}
/*—————————————————–
Output
Enter a string:
The Teacher’s day is the birth day of Dr.S.Radhakrishnan
Frequency of the word ‘the’ is 2
———————————————————*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to accept a string and a substring and check if the substring is present in the given string
  2. C Program to accept two strings and concatenate them i.e.The second string is appended to the end of the first string
  3. C Program to accepts two strings and compare them. Finally it prints whether both are equal, or first string is greater than the second or the first string is less than the second string
  4. c program to find the length of a string without using the built-in function
  5. C program to find the length of a string without using the built-in function also check whether it is a palindrome or not

Leave a Comment

Previous post:

Next post: