/* indexmap.cpp - Matt Mahoney, mmahoney@cs.fit.edu This program reads text from standard input and prints an alphabetically sorted list of words and the number of times each word occurs. A word is defined as a sequence of one or more letters and no other characters. For purposes of counting, differences in case are ignored. For instance, "Don't" is 2 words, "don" and "t". The output is in two columns with the count first. For instance, if input.txt is: Don, don't do that. Then the command: indexmap #include #include #include using namespace std; int main() { // Read the input, parsing into lowercase words. Store the count // for each word in a map, count, such that count[word] is the number // of times the word occurs. char c; string word; map count; while (cin.get(c)) { if (isalpha(c)) word += char(tolower(c)); else if (word != "") { ++count[word]; word = ""; } } // Print the contents of count for (map::const_iterator p=count.begin(); p!=count.end(); ++p) cout << p->second << " " << p->first << "\n"; return 0; }