EmbLogic's Blog

No of words in a sentence

Pgm to find the no of  occurance of a word in a sentence.

int main()

{

char str1[80],str2[20],str3[20];

int i=0,j,count=0;

printf(“Enter the sentence:\n”);gets(str1);

printf(“Enter the word for which no of occurance needs to be checked:\n”);gets(str2);

while(str1[i]!=”)

{

for(j=0;str1[i]!=’ ‘;j++,i++)

str3[j]=str1[i];   //Each word of the sentence is copied to str3 & compared with str2

str3[j]=”;

if(strcmp(str3,str2)==0)

count++;

}

printf(“%s word occured %d times”,str2,count);

}

I am so surprised to see that str2 value has some garbage values aftr execution.

Though count value is incremented properly but still for some particular sentences it shows “count-1″ value.Plz help me to understand where i went wrong.

Thanks

Pushpa

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>