Possible Duplicate:
What is an easy way to tell if a list of words are anagrams of each other?
finding if two words are anagrams of each other
I have below C code written to check if two given strings are anagrams of each other. I know this is worst in terms of complexity/efficiency and there are far many better ways to do it.
#include "stdio.h"
main()
{
char s1[]="mist";
char s2[]="mitt";
int i,j,isanag=0;
if(strlen(s1)!=strlen(s2))
printf("Not anagrams\n");
for(i=0;i<strlen(s1);i++)
{
isanag=0;
for(j=0;j<strlen(s2);j++)
{
if(s1[i]==s2[j])
{
isanag = 1;
break;
}
}
if(isanag == 0)
{
printf("Not anagrams\n");
getch();
exit(0);
}
}
printf("Yes Anagrams\n");
getch();
}
This works fine and prints Not Anagrams which is correct If I swap the names of two strings as below it gives wrong answer
char s1[]="mitt";
char s2[]="mist";
I know the way the 2 for loops are coded this is obvious.
What can I do to improve this code and resolve this quirk?