JAVA STRING ANAGRAM PROGRAM

JAVA  STRING ANAGRAM PROGRAM

/*
Exercise: An anagram is a word or a phrase made by
transposing the letters of another word or phrase;
for example, "parliament" is an anagram of "partial men"
and "Software" is an anagram of "swear oft". Write a
program that figures out whether ine string
is an anagram of another string.
The Program should ignore white space and punctuation.
*/
import java.util.*;
import java.io.*;
class demo
{
    static boolean areAnagram(char []str1, char []str2)
    {
        int n1=str1.length;    int n2=str2.length;
        //System.out.print(n1);
        if(n1!=n2)
            return false;
        Arrays.sort(str1);
        Arrays.sort(str2);
        for(int i=0; i<n1; i++)
        {
            if(str1[i] != str2[i])
                return false;
        }
        return true;
    }
    public static void main(String[] args) {
        char str1[]={'t','e','s','t'};
        char str2[]={'t','t','e','w'};
        if(demo.areAnagram(str1,str2))
            System.out.print("\n The two strings are anagram of each other\n");
        else
            System.out.print("\n The two strings are not anagram of each other\n");
    }
}

Comments

Popular posts from this blog

PALINDROME NUMBER

CONVERT CARTESIAN COORDINATES TO POLAR COORDINATE

FIBONACCI SERIES