Check string is Anagram or not using function with pointer

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


Source code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
char* shorting(char a[10]);
void main()
{
	char a[10],b[10],c[10],d[10],ans;
	int i,j,len_1=0,len_2=0,count=0;
	clrscr();
	printf("Enter the First String:");
	gets(a);
	strcpy(c,shorting(a));
	printf("Enter the Second String:");
	gets(b);
	strcpy(d,shorting(b));

	printf("C String is:%s \n",c);
	printf("D string is:%s \n",d);
	for(i=0;a[i]!=NULL;i++)
	{
		len_1++;
	}
	for(i=0;b[i]!=NULL;i++)
	{
		len_2++;
	}
	if(len_1==len_2)
	{
		for(i=0;i<len_1;i++)
		{
			if(a[i]==b[i])
			{
				count++;
			}
		}
		if(count==len_1)
		{
			printf("Given String is Anagram ");
		}
		else
		{
			printf("Given String is not Anagram ");
		}
	}
	else
	{
		printf("Given String is not Anagram ");
	}
	getch();
}
char* shorting(char a[10])
{
	int i,j,t_1;
	for(i=0;i<a[i]!=NULL;i++)
	{
		for(j=0;j<a[j]!=NULL;j++)
		{
			if(a[i]<a[j])
			{
				t_1=a[i];
				a[i]=a[j];
				a[j]=t_1;
			}
		}
	}
	return a;
}

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 *