Write a java program to check two string is Anagram or not :
import java.util.Scanner;
class Anagram {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter first Sentence ");
String st=sc.nextLine();
int[] string1=indexNumber(st);
System.out.println("Enter Second Sentence ");
String st2=sc.nextLine();
int[] string2=indexNumber(st2);
CompareTwoArray ct=new CompareTwoArray();
boolean rs=ct.isAnagram(string1, string2);
if(rs) {
System.out.println("String is anagram ");
}
else {
System.out.println("Not a anagram");
}
}
static int[] indexNumber(String str) {
int count[]=new int[26];
for(int i=0;i<str.length();i++) {
char ch=str.charAt(i);
if(ch>=65 && ch<=90) {
count[ch-65]++;
}
else if(ch>=97 && ch<=122) {
count[ch-97]++;
}
}
return count;
}
}
public class CompareTwoArray {
static boolean isAnagram(int arr1[],int arr2[]) {
for(int i=0;i<arr1.length;i++) {
if(arr1[i]!=arr2[i]) {
return false;
}
}
return true;
}
}
Comments
Post a Comment