# include # include // // Program 10.8, Stephen Kochan, Programming in C. // int main ( void ); bool alphabetic ( const char c ); void readLine ( char buffer[] ); int countWords ( const char string[] ); int main ( void ) { char text[81]; int totalWords = 0; bool endOfText = false; printf ( "Type in your text.\n" ); printf ( "When you are done, press 'RETURN'.\n" ); while ( ! endOfText ) { readLine ( text ); if ( text[0] = '\0' ) { endOfText = true; } else { totalWords = totalWords + countWords ( text ); } } printf ( "\n" ); printf ( "There are %i words in the above text.\n", totalWords ); return 0; } bool alphabetic ( const char c ) { bool value; if ( ( 'a' <= c && c <= 'z' ) || ( 'A' <= c && c <= 'Z' ) ) { value = true; } else { value = false; } return value; } void readLine ( char buffer[] ) { char character; int i = 0; do { character = getchar ( ); buffer[i] = character; i = i + 1; } while ( character != '\n' ); buffer[i-1] = '\0'; return; } int countWords ( const char string[] ) { int i, wordCount = 0; bool lookingForWord = true; for ( i = 0; string[i] != '\0'; i++ ) { if ( alphabetic ( string[i] ) ) { if ( lookingForWord ) { wordCount = wordCount + 1; lookingForWord = false; } } else { lookingForWord = true; } } return wordCount; }