Anagrams

 


 

import java.util.Scanner;
public class Anagram {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.println("Enter your first string: ");
String string1 = input.nextLine();
System.out.println("Enter your second string: ");
String string2 = input.nextLine();

 

if (isAnagram(sort(string1), sort(string2)) == true){
System.out.println("Anagrams");

}else{
System.out.println("not Anagrams");
}
System.out.println(string1);
System.out.println(string2);

}
/// sort string- by converting String->char->string

public static String sort(String s){
char[] s1 = new char[s.length()];
s1 = s.toCharArray();

java.util.Arrays.sort(s1);
s = new String(s1);
return s;
}

public static boolean isAnagram(String s1, String s2){
return s1.equals(s2);
}

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: