Reputation: 31
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;
}
}
}
}
}
Upvotes: 1
Views: 490
Reputation: 51393
Since you can not use array, one approach can be:
public boolean isAnagram(String a, String b) {
if(a.length() != b.length()) return false;
for (int i=0; i < a.length(); i++) {
for (int j=0; j <b.length(); j++) {
if (a.charAt(i) == b.charAt(j))
{
a = removeCharAt(a,i);
b = removeCharAt(b,j);
i=0;
j=0;
}
}
}
return a.equals(b);
}
where removeCharAt is :
public static String removeCharAt(String s, int pos) {
return s.substring(0,pos)+s.substring(pos+1);
}
The first test is to verify if both strings have the same size. If they have, then you start by taking a char from the first string, and check if this char exists on the second string. If it exist you remove this char from both strings. And take another char from the first string and repeat the process, until there is no more chars to compare. In the end if the final result strings are equal than they are anagram.
You should remove the char of both strings to avoid that for the examples like string a ="AAAAA" and string b ="Abbbb" it give you an anagram. Since each char of string 'a' will match the first position of string 'b'.
Some problem in your code:
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;
}
}
}
}
You will have out of range error since i<=a
inside the for and a = s1.length()
., you are not returning anything in the case of if (a!=b)
. Besides that you are comparing both string, by comparing if each char on the same position are equal. This approach will no work for searching for an anagram, it may work to search if two string are equal.
Upvotes: 3
Reputation: 89
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:
public static boolean isAnagram(String s1, String s2) {
char[] string1 = s1.toCharArray();
char[] string2 = s2.toCharArray();
Arrays.sort(string1);
Arrays.sort(string2);
return Arrays.equals(string1, string2);
}
Upvotes: 1
Reputation: 8959
You could need to declare a boolean type isAnagram in the method and set it to true or false at the while, e.g.
boolean isAnagram;
// Do other stuff
while (count == a)
{
isAnagram = true;
}
isAnagram = false;
}
}
}
return isAnagram;
}
otherwise you'll get compilation errors
Upvotes: -2