Rearranging a word to create an anagram

An anagram is a word that is produced by rearranging the letters of the word itself. Let's take a look at the word ELBOW. We can say that BELOW is anagram of ELBOW, since BELOW uses all the original letters of ELBOW exactly once. Not only from one word, an anagram can also be created from, and can create, two or more words: SCHOOL MASTER is an anagram of THE CLASSROOM, or FOURTH OF JULY is an anagram of JOYFUL FOURTH.

Checking whether two strings are an anagram or not is quite easy. We just need to sort the two and compare the sorted strings. They are an anagram if both sorted strings are exactly the same. Take a look at the following C++ code:

bool IsAnagram(    string str1,    string str2){ // Anagram is ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.