Check string is Anagram or not using function

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


Source code:

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

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 *