Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 64 / 68

Testing whether two words are anagrams

This lesson covers testing for anagrams in Objective-C, with the aim of understanding how to sort and compare strings.

An anagram is a word made by rearranging the letters of another (listen and silent, for instance). Sort the letters of both and compare.

The sample code lower-cases with lowercaseString, takes the characters one at a time into an NSMutableArray, sorts, and compares. "listen" and "silent" have the same letters, so sorted they match.

A common early stumble is not allowing for case. The sample unifies case with lowercaseString before comparing; forget that and the same word in different cases is judged different.

It is a basic technique for comparing strings while ignoring order, and it extends to simple cipher-solving games.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)