Friday, September 10, 2010

 Hi, this is my version of programming a anagram program in Java language.... I hope this will help... God bless.

import java.util.*;
import java.lang.*;
public class anagrams {

   
    public static void main (String[] args) {
        Scanner eric=new Scanner(System.in);
        String array1, array2;
        char a='*';
        int e=0;
        boolean val=true;
        System.out.print("Input 1: ");array1=eric.nextLine();
        System.out.print("Input 2: ");array2=eric.nextLine();
        array1=array1.toLowerCase().replaceAll("\\W", "");
        array2=array2.toLowerCase();
        Boolean checker[]=new Boolean[array1.length()];
           for (int i = 0; i<array1.length(); i++){
            for (int j = 0; j<array2.length(); j++){
                if(array1.charAt(i)==array2.charAt(j)){
                    checker[i]=true;
                    for (e = j; e<array2.length()-1; e++);
                    if(array1.charAt(i)==array2.charAt(e)){
                        checker[i]=false;
                    }else{
                        checker[i]=true;
                    }                   
                    continue;
                }else{
                    checker[i]=false;
                }
            }       
        }
        for (int i = 0; i<checker.length; i++){
            if(checker[i]==false){
                val=false;
            }
        }
        if(val){
            System.out.print("There're anagrams!");
        }else{           
            System.out.print("There're not anagrams!");
        }
       
       
       
       
}
   
}

No comments:

Post a Comment