Two strings are anagram if they are written using same exact letters,ignoring space, punctuation and capitalization. Each letter should have the same count in both strings.

PROGRAM: ANAGRAM STRINGS

 Two strings are anagram if they are written using same exact letters,ignoring space, punctuation and capitalization. Each letter should have the same count in both strings.

 For Example:Mary and Army are anagram of each other.

 SYNTAX:
import java.io.*;
class anagram
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Two Strings to check whether they are anagram or not");
        String s1=br.readLine();
        String s2=br.readLine();
        String nstr="",ntr="";int count=0;
        for(int i=0;i less than s1 length;i++)
        {
            if(Character.isLetter(s1.charAt(i)))
            {
                nstr+=s1.charAt(i);
            }
        }
        for(int i=0;i less than s2 length;i++)
        {
            if(Character.isLetter(s2.charAt(i)))
            {
                ntr+=s2.charAt(i);
            }
        }
        nstr=nstr.toLowerCase();
        ntr=ntr.toLowerCase();
        for(int i=0;i less than nstr length;i++)
        {
            for(int j=0;j less than ntr length;j++)
            {
                if(nstr.charAt(i)==ntr.charAt(j))
                {
                    count++;
                    break;
                }
            }
        }
        if(count==nstr.length())
        {
            System.out.println("Strings are anagram");
        }
        else
        System.out.println("Strings are not anagram");
    }
}

No comments:

Post a Comment