Assignment 2015

Input two strings and check whether they are Anagram or not.

import java.io.*;
  class anagram
  {
    public static void main(String args[])throws IOException
    {
      String s,r;
      char ch1,ch2;
      int i,j,f,p;
       BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

      System.out.print("Enter first word");
        s=in.readLine();
      System.out.print("Enter second word");
        r=in.readLine();

      if(s.length()!=r.length())
       System.out.print("Not anagram");
      else
      {
         p=0;
      for(i=0;i<s.length();i++)
      {
        ch1=s.charAt(i);
         f=0;
        for(j=0;j<r.length();j++)
         {
           ch2=r.charAt(j);
           if(ch1==ch2)
            {
             f=1;
             break;
            }
         }
         if(f==0)
          {p=1;
           break;
          }
      }
        if(p==0)
          System.out.print("Anagram");
        else
         System.out.print("not");
    }
}
}

1 comment: