Anagram

Java program to check if two String are anagram of each other

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;


public class Anagram {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s1 = br.readLine();
        String s2 = br.readLine();

        if (s1.equals(s2)) {
            System.out.println("true");
        } else {
            s1 = s1.replace(" ", "");
            s2 = s2.replace(" ", "");
            char c1[] = s1.toCharArray();
            char c2[] = s2.toCharArray();
            Arrays.sort(c1);
            Arrays.sort(c2);
            if (new String(c1).equals(new String(c2))) {
                System.out.println("Its an anagram");
            } else {
                System.out.println("Its not an anagram");
            }
        }
    }
}



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