C# – Anagram Checker

Created by
Wednesday, August 3, 2016

So what is an anagram!? : An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for exampleDoctor Who can be rearranged into Torchwood. Someone who creates anagrams may be called an “anagrammatist”.[1] The original word or phrase is known as the subject of the anagram. (wiki)

With this simple C# application you can gain see how a program can check for a correct anagram and output the true or false result based on whether or not your input string is an anagram or not. This is an enough to follow demonstration. Please find the source code solution attached along with a brief video demonstration of the program in action.

ssCheckAnagram.csC#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnagramCheck
{
    class ClassCheckAnagram
    {
        public int check_anagram(char[] a, char[] b)
        {
            Int16[] first = new Int16[26];
            Int16[] second = new Int16[26];
            int c = 0;

            for (c = 0; c < a.Length; c++)
            {
                first[a[c] - 'a']++;
            }

            c = 0;

            for (c = 0; c < b.Length; c++)
            {
                second[b[c] - 'a']++;
            }

            for (c = 0; c < 26; c++)
            {
                if (first[c] != second[c])
                    return 0 ;
            }

            return 1;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AnagramCheck
{
    class ClassCheckAnagram
    {
        public int check_anagram(char[] a, char[] b)
        {
            Int16[] first = new Int16[26];
            Int16[] second = new Int16[26];
            int c = 0;
 
            for (c = 0; c < a.Length; c++)
            {
                first[a[c] - 'a']++;
            }
 
            c = 0;
 
            for (c = 0; c < b.Length; c++)
            {
                second[b[c] - 'a']++;
            }
 
            for (c = 0; c < 26; c++)
            {
                if (first[c] != second[c])
                    return 0 ;
            }
 
            return 1;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AnagramCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            ClassCheckAnagram cca = new ClassCheckAnagram();
            Console.WriteLine("Enter first string\n");
            string aa = Console.ReadLine();
            char[] a = aa.ToCharArray();
 
            Console.WriteLine("\nEnter second string\n");
            string bb = Console.ReadLine();
            char[] b = bb.ToCharArray();
            int flag = cca.check_anagram(a, b);
 
            if (flag == 1)
                Console.WriteLine("\nThey are anagrams.\n");
            else
                Console.WriteLine("\nThey are not anagrams.\n");
            Console.ReadKey();
        }
    }
}

Hope everything is easy enough to understand. I will come back to this post in more detail with a follow on anagram program written also in C#.

0
Rating
1 2 3 4 5

Save

Saved

Report

Reported
Reasons
Cancel
0 comments