// // test of split routine // // Described in Chapter 7 of // Data Structures in C++ using the STL // Published by Addison-Wesley, 1997 // Written by Tim Budd, budd@cs.orst.edu // Oregon State University // # include # include "simstring.h" // or use # include void split (string & text, string & separators, list & words) // split a string into a list of words // text and separators are input, // list of words is output { int textLen = text.length(); // find first non-separator character int start = text.find_first_not_of(separators, 0); // loop as long as we have a non-separator character while ((start >= 0) && (start < textLen)) { // find end of current word int stop = text.find_first_of(separators, start); if ((stop < 0) || (stop > textLen)) stop = textLen; // add word to list of words words.push_back (text.substr(start, stop - start)); // find start of next word start = text.find_first_not_of (separators, stop+1); } } void main() { string text = "it was the best of times, it was the worst of times."; string smallest = "middle"; string largest = "middle"; list words; string separators = " .,!?:"; split(text, separators, words); list::iterator current; list::iterator stop = words.end(); for (current = words.begin(); current != stop; ++current) { if (*current < smallest) smallest = *current; if (largest < *current) largest = *current; } cout << "smallest word " << smallest << endl; cout << "largest word " << largest << endl; }