import java.util.*;
public class Anagram {
//quite complex cos i was using array for comparison
public static void main(String me[])
{
String str1 =new String();
String str2 =new String();
char c1[] = new char[100];
char c2[] = new char[100];
int size1, size2;
int match = 0;
Scanner in = new Scanner(System.in);
System.out.println(" Enter First Word: ");
str1 = in.nextLine();
System.out.println(" Enter Second Word: ");
str2 = in.nextLine();
size1 =str1.length(); //take the length of the first word
size2 =str2.length(); //take the length of the second word
System.out.println("sizes: " + size1 + "," + size2);
if (size1 != size2)
{
System.out.println("Words are not of same lengt - NOT ANAGRAMS");
}
else
{
c1 = str1.toCharArray(); //takes word1 to char1
c2 = str2.toCharArray();// takes word2 to char2
int flag;
flag = 0;
for (int i=0;i<size1;i++)
{
for (int k=0;k<size1;k++)
{
// System.out.println(c1[i] + " ");
if (c1[i] == c2[k])
{ flag = 1;} //move to next letter
}
if (flag == 1){ match++;}
}
}
System.out.println("match: " + match);
if (match == size1)
{ System.out.println("the words " + str1 + " and " + str2 + " are Anagrams!"); }
else
{
System.out.println("the words " + str1 + " and " + str2 + " are NOT Anagrams!");
}
}
}
}
No comments:
Post a Comment