5
I need to buy two strings and check if the second one is an anagram of the first one. I thought I’d use FOR repeat loops. I think you should first check if both are the same size, then go through them and check if each letter of the first one exists also in the second one. If they all exist, one is the other’s anagram. I just don’t know how to do this...
public static void main(String[] args) {
Scanner ent = new Scanner(System.in);
String s, r;
int i, j;
System.out.println("Digite a palavra/frase:");
// crio as strings
s = ent.nextLine();
r = ent.nextLine();
// verifico se têm o mesmo tamanho
if (s.length()==r.length()) {
// percorro ambas verificando se cada letra da 1ª existe na 2ª
for (i=0; i<s.length(); i++) {
for (j=0; j<r.length(); j++) {
// não sei continuar...
}
}
}
}
An alternative way to do it is to sort the letters of each word in alphabetical sequence and check if they are equal.
– Piovezan
I noticed that during the acceptance you first accepted my answer and then Victor’s. I don’t know if you know that acceptance, unlike the vote, can only be made for one answer in each question. And acceptance is the last one you click. I don’t know if your intention was to actually choose Victor’s answer, which is a good answer, I would have no problem accepting it, or choosing mine. No matter which one you choose, the decision is yours, but it is important to understand how the tool works and make a choice according to your real will.
– Maniero
@bigown This happened because I first saw your answer and I already accepted it, then I saw Victor’s and I thought she was more suited to the style of programming that I use. Your solution is good too (if I could, I would keep the vote on both, even because they are very similar), but it uses modularization, which I still do not understand very well nor know how to use right. Anyway, thank you for your help and for worrying about explaining about the working of the tool :)
– Cristiane Dos Santos Costa