////////////////////////////////////////////////////////////////// // // printfilewords.cpp // // Started by Jeff Ondich on 1/3/00 // Last modified 2/16/00 // // This program retrieves words from a file specified by // the user and prints them, one per line, to cout. // // 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 ) { // Open the file requested by the user. string fileName; cout << "What file do you want to process? "; cin >> fileName; ifstream inputFile( fileName.c_str() ); if( !inputFile.is_open() ) { cout << "Can't open " << fileName << ". Perhaps it doesn't exist." << endl; exit( 1 ); } // Print out the words in the file. cout << "Here are the words in the file:" << endl; string word; while( inputFile >> 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; }