Anagram

Algorithm

Input   Two string s1 and s2.  
Output  S1 is anagram of s2 or not.  
complexity  O(n).  
  
Anagram(s1,s2)   
1.  [Initialize segment variables] MAX_ASCII := 127, TRUE := 1 and  FALSE :=0.  
2.  Set freq[i] : = 0 for i=0 to MAX_ASCII  
3.  Repeat while(*s1)  
Set   freq[(INT) *s1++]++.  
4.  Repeat while(*s2)  
Set freq[(INT) *s2++]--.  
5.  Repeat for i=0 to MAX_ASCII  
If (freq [i])   
then : return FALSE.  
6.  Return TRUE.  
7.  Exit.  

Algorithm Description

1. Algorithm inputs two words s1 and s2 and checks whether the second word is anagram of first
2. freq is used for storing the occurrences of letter in word

C Code

//Program to check wheather a given word is anagram of another or not?

#include<stdio.h>
#include<conio.h>
int main()
{
	char a[50],b[50],i,j,x1,x2,s1,s2,t;
	printf("Enter first Word ");
	scanf("%s",a);
	printf("Enter Second Word ");
	scanf("%s",b);
	i=0;
	while(a[i]!='\0')
		i++;
	j=0;
	while(b[j]!='\0')
		j++;
	if(i!=j)
		printf("Length not matched ");
	else
	{
		x1=1;
		s1=0;
		i=0;
		while(a[i]!='\0')
		{
			t=(a[i]%96);
			x1=x1*t;
			s1=s1+t;
			i++;
		}
		x2=1;
		s2=0;
		i=0;
		while(b[i]!='\0')
		{
			t=(b[i]%96);
			x2=x2*t;
			s2=s2+t;
			i++;
		}
		if(x1==x2&&s1==s2)
		printf("Yes it is an Anagram");
		else
		printf("Not an anagram");
	}
	return 0;
}