// Anagram checking program. #include <mstring.h> // defines line type (character string ended by newline) #include <vector.h> #include <algo.h> void lowercase(line& line1) // Convert all uppercase letters in line1 to lowercase { for (int i = 0; line1[i] != '\0'; ++i) if ('A' <= line1[i] && line1[i] <= 'Z') line1[i] += 'a' - 'A'; } void remove_blanks(line& line1) // Squeeze out all blank characters in line1 { line::iterator i = remove(line1.begin(), line1.end(), ' '); // i now points just past the end of retained characters line1.erase(i, line1.end()); } void standardize(line& line1) // Put line1 in a standard form: all lowercase, no blanks, sorted { lowercase(line1); remove_blanks(line1); sort(line1.begin(), line1.end() - 1); } bool anagrams(line line1, line line2) // Returns true if line1 and line2 are anagrams, // ignoring all blanks and case of letters { standardize(line1); standardize(line2); return line1 == line2; // true if lines are equal in all corresponding character positions } int main() // Repeatedly read two lines and test whether they are anagrams. // Terminate when first line has two or fewer characters before newline. { while (true) { line line1, line2; // line is a type defined in mstring.h cin >> line1; // keeps newline and appends null character cin >> line2; if (line1.size() < 4) /* terminate if less than 2 characters (besides newline and null) */ break; cout << line1; cout << line2; if (anagrams(line1, line2)) cout << "Yes, the above two lines are anagrams!\n"; else cout << "No, the above two lines are not anagrams.\n"; cout << "\n\n\n"; } return 0; }