- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if two Strings are anagrams of each other using C#?
Under anagram, another string would have the same characters present in the first string, but the order of characters can be different.
Here, we are checking the following two strings −
string str1 = "silent"; string str2 = "listen";
Convert both the strings into character array −
char[] ch1 = str1.ToLower().ToCharArray(); char[] ch2 = str2.ToLower().ToCharArray();
Now, sort them −
Array.Sort(ch1); Array.Sort(ch2);
After sorting, convert them to strings −
string val1 = new string(ch1); string val2 = new string(ch2);
Compare both the strings for equality. If both are equal, that would mean they are anagrams.
The following is the code −
Example
using System; public class Demo { public static void Main () { string str1 = "silent"; string str2 = "listen"; char[] ch1 = str1.ToLower().ToCharArray(); char[] ch2 = str2.ToLower().ToCharArray(); Array.Sort(ch1); Array.Sort(ch2); string val1 = new string(ch1); string val2 = new string(ch2); if (val1 == val2) { Console.WriteLine("Both the strings are Anagrams"); } else { Console.WriteLine("Both the strings are not Anagrams"); } } }
Output
Both the strings are Anagrams
- Related Articles
- Check if two strings are anagram of each other using C++
- Write a program in JavaScript to check if two strings are anagrams of each other or not
- C# program to determine if Two Words Are Anagrams of Each Other
- Check whether two strings are anagram of each other in Python
- A Program to check if strings are rotations of each other or not?
- Check if strings are rotations of each other or not in Python
- How to check if two strings are equal in Java?
- Program to check strings are rotation of each other or not in Python
- Check if all levels of two trees are anagrams or not in Python
- C Program to check if two strings are same or not
- Java Program to Check if two strings are anagram
- Golang Program to Check if two Strings are Anagram
- Are the strings anagrams in JavaScript
- Python - Check if two strings are isomorphic in nature
- How to check whether the given strings are isomorphic using C#?

Advertisements