/* * Read a text file whose name is given on the command line, and for each word: * if it is an integer, insert it into an array in sorted order * if it is not an integer, insert it into an array of words. * * Notes: converted to use C++ strings, because C strings are messier. * Need to grow arrays, need to insert in sorted order. * Growing arrays might be done the way we grew a C string: * * bigger = new [size+1] * for(i=0; i #include #include #include using namespace std; int mywhite(char c) { /* Too bad you can't say: * if (c == (' ' || '\t' || '\n')) return 1; * If you learned my language, Unicon, from unicon.org, you could do that. * * In C, you can sort of get a similar effect from * if (strchr(" \t\n", c)) * And here is another way to do it: */ switch (c) { case ' ': case '\t': case '\n': return 1; } return 0; } int is_integer(string s) { if (s.size() == 0) return 0; for(int i=0; i> because >> skips over whitespace c = f.get(); while (! f.eof()) { // read one word worth of the input file // skip over white space until we have at least one non-whitespace char while (mywhite(c)) { if (f.eof()) break; // not >> because >> skips over whitespace c = f.get(); } if (f.eof()) { // we are end of file, but might have seen a non-white character // as our final one, might need to add logic here. break; } // to get here, we must have found a non-whitespace character, // so we have a word coming. // if it is whitespace we are done. for ( ; !mywhite(c); ) { // add the character to the end of our current "word" theString = theString + c; size++; // read another character, IF YOU CAN if (f.eof()) break; // not >> because >> skips over whitespace c = f.get(); } if (is_integer(theString)) { cout << theString << " is an integer" << endl; } else { cout << theString << " is something else" << endl; } // start the next word size = 0; theString = ""; } }