Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 64 / 68

Checking Whether Two Words Are Anagrams

In this lesson you will learn how to check whether two words are anagrams in C#, covering how to sort and compare strings. This is for anyone searching for "C# anagram check implementation".

An anagram is a different word made from the same letters rearranged (for example, listen and silent). Sort the letters of both strings and see whether they match.

The sample code lower-cases and sorts the letters with a.ToLower().OrderBy(c => c), then compares the two sorted sequences with .SequenceEqual(). "listen" and "silent" are made of the same letters, so they match once sorted.

A common stumbling block for beginners is not allowing for case differences. The sample folds to lower case with ToLower() before comparing; skip that and the same word in different cases is judged to be different.

It looks like a word game, but it is useful, simple practice in real string comparison. For long strings, counting how often each character occurs is faster.

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

๐Ÿงช This site can't compile or run 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)