/* Program to count the number of occurances of a word in a string */ /* Problem 23 */ #include #include #include main() { char str[80]; char search_word[10]; int i,j,k,l,len1,len2; int found,cnt,status; clrscr(); printf("Please enter the string here : "); gets(str); len1 = strlen(str); printf("Enter the search_word : "); scanf("%s",search_word); len2 = strlen(search_word); j = 0; cnt = 0; status = 0; for (i = 0; i < len1 ; i++) { k = i; if (str[i] == search_word[j]) { found = 1; for (l = 0; l < len2; l++) { if (str[k] != search_word[l]) { found = 0; break; } k++; } if (found == 1) { cnt++; i += len2; status = 1; } } } if (status == 1) printf("\nThe word was found %d times",cnt); else printf("\nSorry the word could not be found"); printf("\n\nPress any key to exit..."); getch(); }