C Program to check whether two strings are anagram.

Anagram example

This article will cover all the questions related to anagram and logic to write its code in easy way.

What is anagram ?


An anagram is a combination of words created by rearranging the letters of the original word to make a new word or phrase.
Its examples can be a great fun and interesting , and they often end to crazy results. The word funeral can be turned into real fun.

What is history of anagram ?

People usually make anagrams just for fun purpose, but sometimes they are used as pseudonyms or some codes. It can be explained with example, the French writer Francois Rabelais published his controversial first book under Alcofribas Nasier, an anagram of his name.
If you want to know more about history then you can check here.

What is an example of an anagram?


The most funniest examples are the ones where the rearranged letters make some sort of comment on the original. “Dormitory” turns into the anagram “dirty room,” and “snooze alarms” can be rearranged into “Alas! No more Zs.”.
In these example meaning of the word changes but their characters remains the same .
Some other examples are shown below :

Dormitory = Dirty room.
School master = The classroom.
Conversation = Voices rant on.
Listen = Silent.
Astronomer = Moon starer.
The eyes = They see.
A gentleman = Elegant man.
Funeral = Real fun.

Implementation of Anagram in C

#include <stdio.h>
#include <string.h>
int main()
{

char a[100],b[100];
int i,n,p,j,count=0;
printf("Enter 1st string :");
scanf("%s",a);
printf("Enter 2nd string :");
scanf("%s",b);
n=strlen(a); //find length of first string
p=strlen(b); //find length of second string
if(n==p)
{
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(a[i]==b[j])  //compare each letters of both string
            {
                count++;
                break;
            }
        }
    }
    if(count==n) //check if count is same as length of string 
    {
        printf("They are anagram");
    }
    else
    {
        printf("They are not anagram");
    }
}
else
{
    printf("Both string should be of same size");
}
}
Output :
Enter 1st string : technoname
Enter 2nd string : nametechno
They are anagram

If you know some other way to write this program then you can leave a comment here or mail us to technonamecontact@gmail.com , So anyone can a get chance to publish their code in our website.

Please check most asked programming questions of strings with examples by clicking here, to get a deep knowledge about string operations.

For understanding data structures in a easy way you can check this article in our website.

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

Leave a Reply

Your email address will not be published. Required fields are marked *