w3resource

C# Sharp Exercises: Check if a string is an anagram of another given string

C# Sharp String: Exercise-58 with Solution

From Wikipedia-
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode.

Write a C# Sharp program to check if two given strings are anagrams or not.

Sample Data:
("wxyz", "zyxw") -> True
("pears", "spare") -> True
("stone", "tones") -> True
("cat", "rat") -> False

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text1 = "wxyz";
            string text2 = "zyxw";
            Console.WriteLine("Original strings: " + text1 + "  " + text2);
            Console.WriteLine("Check if two said strings are anagrams or not: " + test(text1, text2));
            text1 = "pears";
            text2 = "spare";
            Console.WriteLine("\nOriginal strings: " + text1 + "  " + text2);
            Console.WriteLine("Check if two said strings are anagrams or not: " + test(text1, text2));
            text1 = "stone";
            text2 = "tones";
            Console.WriteLine("\nOriginal strings: " + text1 + "  " + text2);
            Console.WriteLine("Check if two said strings are anagrams or not: " + test(text1, text2));
            text1 = "cat";
            text2 = "rat";
            Console.WriteLine("\nOriginal strings: " + text1 + "  " + text2);
            Console.WriteLine("Check if two said strings are anagrams or not: " + test(text1, text2));
         }

         public static bool test(string text1, string text2)
           {
            string stext1 = String.Concat(text1.ToUpper().OrderBy(c => c));
            string stext2 = String.Concat(text2.ToUpper().OrderBy(c => c));
            
            if (stext1 == stext2)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }

        }
    }

Sample Output:

Original strings: wxyz  zyxw
Check if two said strings are anagrams or not: True

Original strings: pears  spare
Check if two said strings are anagrams or not: True

Original strings: stone  tones
Check if two said strings are anagrams or not: True

Original strings: cat  rat
Check if two said strings are anagrams or not: False

Flowchart :

Flowchart: C# Sharp Exercises - Check if a string is an anagram of another given string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Reverse the words of three or more lengths in a string.
Next: Reverse an integer and add with the original number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter