here is my code. but it doesnt work. please help me about where is my problem. it is very complicated for me. my code must find that my 2 inputs are anagram or not. (ex: silent and listen)
import java.util.Scanner ;
import java.lang.String ;
public class Anagram {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in) ;
System.out.println("Enter your firs word: ");
String s1= scanner.nextLine() ;
System.out.println("Enter your second word: ");
String s2 = scanner.nextLine() ;
if(isAnagram(s1,s2)){
System.out.println("Your words are anagram") ;
}
}
public static boolean isAnagram(String s1, String s2) {
int a = s1.length() ;
int b= s2.length() ;
if (a==b){
int count=0;
int i,j ;
char x, y ;
for (i=0, j=0; i<=a; i++){
x = s1.charAt(i) ;
y = s2.charAt(j);
j++;
if (x==y){
count++ ;
while (count==a){
return true;
}
return false;
}
}
}
}
}
I would propably try to sort the Strings and then check if they have the same letter at the same position. Here would be a simple solution with java.util.Arrays: