• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anagram Comparison Help  RSS feed

 
Alex Longazo
Greenhorn
Posts: 1
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I'm making a program in which the user enters two words and it is supposed to tell you whether or not the two words are an anagram or not. I'm able to store the two words into char arrays but I'm having problems with comparing the two words. The logic seems like it should work but it doesn't and I have no idea why :/ I'll post my code below and I'll put in bold the part that I think is the problem. Any help would be greatly appreciated. (:

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch! I took a quick look at the code, and whereas I did not specifically see what is wrong, I saw enough to make some general comments.

1) An anagram is "the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once" according to Wikipedia.

If you are talking about single words only, then you know some key facts - the main one being the two strings need to be exactly the same lengths, since they both have to use the same characters. So your first comparison where you check if one is larger then the other comes to the wrong conclusion: it shouldn't ask the user to reverse the order, order doesn't matter, you know they are not anagrams.

If you allow phrases, then typically spaces don't count (nor, do I think does punctuation). You should do something to remove these extra, non-affecting characters from the strings.

2) In the middle of your code you have two redundant loops. You do:

That could be replaced by:

You do the same for w2/word2.

3) The rest of your code is the 'meat' of the algorithm. Unfortunately I think you have made it quite a bit more complicated than it needs to be. I would suggest you stop writing code. Start with a pencil and paper and work out how you would figure out if a word is an anagram in your native language. If it helps, write a couple words on a paper, then go through the process of checking them for being anagrams manually. Do it slowly (no cheating and peaking ahead, take careful consideration of each character you look at) and for everything you do (including 'looking at a character') write it down the step in English (or your native tongue). Only when you have concise, simple steps you could give to a ten year old as instructions to solve the problem should you then start to code (afterall the computer is pretty simple, it only does the exact one at a time, exactly as you wrote them).>
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
Unlike Steve, I didn’t read your code. It looks complicated, so I simply presumed it was wrong. I did notice you are using != true. Never use == != true false in any combination. Not only is it poor style, but also error‑prone. What if you write = by mistake? If you want something to be false you write if (!boo)…, not if (boo != true)…
I also added code tags, which you should always look. See how much better the code looks now. You can’t use bold tags inside code tags; the bit highlighted was lines 26-35.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!