////////////////////////////////////////////////////////////////// // // printwords.cpp // // Started by Jeff Ondich on 1/3/00 // Last modified 1/3/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. // // You can run this program in two ways: // // (1) printwords < somefile.txt // // (2) printwords // // In the second case, you will type the input. You // can type as many lines as you like. To terminate // the input, type CTRL-D at the start of a new line. // // Note that CleanWord might take the string it is // given and clean it right out of existence. What // could you change in main() to avoid printing empty // words? // ////////////////////////////////////////////////////////////////// #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; }