/* File: count_blue1.c Count the number of times that the word "blue" appears in an input string. */ #include #include #include #include int count_blue (char * str) ; int main() { char *str ; int n ; str = readline("Enter a string: ") ; n = count_blue(str) ; printf("The world \"blue\" appeard in \"%s\" %d times\n", str, n) ; // deallocate memory free(str) ; return 0 ; } // Count # of times the substring "blue" appears // in the string str. int count_blue(char *str) { int count = 0, i = 0 ; while (1) { // skip over chars until 'b' found while(str[i] != '\0' && str[i] != 'b') { i = i + 1 ; } // if end of string reached if (str[i] == '\0') return count ; // Otherwise found 'b' i++ ; if (str[i] != 'l' ) continue ; // Otherwise found "bl" i++ ; if (str[i] != 'u') continue ; //Otherwise found "blu" i++ ; if (str[i] != 'e') continue ; count++ ; } }