Monday, November 18, 2013

Anagram

An anagram is a word made by transposing the letters of another word ; for example, "software" is an anagram of "swearoft".Write a program that figures out whether one string is an anagram of another string.

SOLUTION: 

  
import java.util.Scanner;

public class Anagram {
 public static void main(String args[]){
  
  Scanner input=new Scanner(System.in);
  System.out.println("Enter any string:");
  String str1=input.next();
  
  System.out.println("Enter the shuffeled string: ");
  String str2=input.next();
  
  char c=0;
  boolean flag=false;
  for ( int i=0; i < str1.length() && str1.length()==str2.length(); i++ ) {
   c=str1.charAt(i);
   if(str2.contains(Character.toString(c)))
    flag = true;
   else{
    flag=false;
    break;
   }
  }
  
  if(flag)
     System.out.println("Its a anagram");
  else
   System.out.println("Its not an angram");
 }
}

No comments:

Post a Comment