Check string is Anagram or not

Write a program to enter two user define strings and check whether given string is Anagram or not.


Source code:

#include<stdio.h>
#include<conio.h>
void main()
{
	char a[10],b[10];
	int i,j,temp=0,length=0,length_1=0,temp_1=0,count=0;
	clrscr();
	printf("Enter the First String:");
	gets(a);
	for(i=0;a[i]!=NULL;i++)
	{
		length++;
	}
	printf("Enter the Second String:");
	gets(b);
	for(i=0;b[i]!=NULL;i++)
	{
		length_1++;
	}
	for(i=0;i<length;i++)
	{
		for(j=0;j<length;j++)
		{
			if(a[i]<a[j])
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	}
	printf(" \n First Sorted String is:%s \n",a);
	for(i=0;i<length_1;i++)
	{
		for(j=0;j<length_1;j++)
		{
			if(b[i]<b[j])
			{
				temp_1=b[i];
				b[i]=b[j];
				b[j]=temp_1;
			}
		}
	}
	printf(" \n Second Sorted string is: %s \n",b);
	if(length==length_1)
	{
		for(i=0;i<length;i++)
		{
			if(a[i]==b[i])
			{
				count++;
			}
		}
		if(count==length)
		{
			printf("Given String is Anagram");
		}
		else
		{
			printf("Given String is not Anagram");
		}
	}
	else
	{
		printf("Given String is not Anagram ");
	}
	getch();
}


Output:

Enter the First String: army
Enter the Second String: mary

First Sorted String is: amry
Second Sorted String is: amry

Given String is Anagram



Enter the First String: jain
Enter the Second String: mary

First Sorted String is: ajin
Second Sorted String is: amry

Given String is not Anagram

Leave a Reply

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