Tuesday, 17 May 2022

C# code to check if two strings are anagrams

 Let's discuss what is Anagram. So in general terms two strings can be said to anagram of each other if  they have same characters irrespective of their order. For example ABCD is anagram of CBDA or DABC. In general we can say LISTEN is anagram of SILENT or POST is anagram of OPTS. So you can find many such words.

Technically how we can do this? So there are several methods available and you can think of any. Here I am going to show one method.

In this method, we will change the casing of each string to either lower or upper so that we can compare them. Now as we all know string is actually an array of characters, so we can sort these string using Array's inbuilt method Array.Sort and after sorting we can easily use the equal operator to check if both strings are equal or not. If they are equal then we can say strings are anagram of each other else not.

Lets see the below code.

There are many other methods through which you can solve this . Try and let us know in the comment section.

Happy Coding


Post a Comment