First off, yeah I know there are many codes out there for this, but how can one learn without doing.
Ok
So we need to write a code to see whether two words are anagrams of one another.
Here is what I have, it compiles but does not work.
The main thing is that when I run the program I want to be able to write in the prompt:
"java Anagram <phrase1> <phrase2>" and then it will tell me.
THank you for any help/suggestions
public class Anagram
{
private String phrase1;
private String phrase2;
private char p1array[];
private char p2array[];
public void main (String[]arg)
{
String phrase1 = arg[0];
String phrase2 = arg[1];
letterchange(phrase1);
letterchange(phrase2);
if (isAnagram())
{
System.out.println(phrase1+" is an anagram of "+phrase2);
}
else
{
System.out.println(phrase1+" is not an anagram of "+phrase2);
}
}
//make letters all lower case
public void letterchange(String phrase)
{
phrase = phrase.toLowerCase();
}
//checks to see if all the letters are the same
public boolean isAnagram()
{
p1array = phrase1.toCharArray();
java.util.Arrays.sort(p1array);
p2array = phrase2.toCharArray();
java.util.Arrays.sort(p2array);
if(p1array.equals(p2array))
{
return true;
}
else
{
return false;
}
}
}