////////////////////////////////////////////////////////////////// // // printwords.cpp // // Started by Jeff Ondich on 1/3/00 // Last modified 1/11/00 // // This program retrieves words from standard input // until reaching end of file, printing all the words // (stripped of punctuation and made lower case) // to the screen. // // 1. Compile this program and try running it in // two different ways. // // (a) Use gEdit to create a text file called // (for example) somefile.txt, with // any kind of text in it. Then, at the // prompt, type: // // printwords < somefile.txt // // (b) Type in response to the prompt: // // printwords // // Then, type whatever bunch of words you // wish, on as many lines as you wish. When // you're done typing, type CTRL-D at the // start of a new line. // // 2. Try running this program on input data // that includes a "word" that consists // entirely of digits and punctuation. // What does CleanWord() do to such a word? // How could you change the main program to // prevent the program from printing these // kinds of words? // // 3. You may use this program as a starting point // for Friday's assignment. A good first goal // would be this: what do you add to the main // program to enable you to count the words in // the input? Good luck. // ////////////////////////////////////////////////////////////////// #include #include void CleanWord( string& theWord ); int main( void ) { string word; while( cin >> word ) { CleanWord( word ); cout << word << endl; } return( 0 ); } ////////////////////////////////////////////////////////////////// // // CleanWord removes all non-alphabetic characters // from the given string (except for hyphens and // apostrophes), and reduces all upper case letters // to lower case. Thus, once CleanWord is done, theWord // consists entirely of lower case letters, hyphens, // and apostrophes. // ////////////////////////////////////////////////////////////////// void CleanWord( string& theWord ) { string s = ""; int originalLength = theWord.length(); for( int i=0; i < originalLength; i++ ) { char c = theWord[i]; if( isalpha(c) || c == '\'' || c == '-' ) s += tolower(c); } theWord = s; }