This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ignoring space in an anagram solver

 
Joanna Spence
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am building an anagram solver and some of the test files contain phrases and I need to know how to 'ignore" the spaces?
I tried the .trim() function but i'm still getting the same errors...

any help would be appreciated.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elmira,
You would either have to loop through the string to remove blanks or use a regular expression to remove any whitespace.

Trim only handles whitespace at the beginning or end of a string. The two above methods handle it anywhere.
 
Joanna Spence
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i figured out if i use the .replace() function with string.replace(" ","") it removes the spaces from in between the words, but now my anagram solver is having trouble finding matches because the string is all the words bunched together.

How would i ignore the space but still be able to successfully search for anagrams?
 
colin shuker
Ranch Hand
Posts: 750
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you give a concrete example of what you want to do.
Its not very clear for me, but I reckon I can help you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As you have been told on your other thread, you need to work out what algorithm you are using to see whether words are anagrams of each other. Write down carefully what you are doing, until you get it all in words of one syllable. You need a pencil, paper and eraser

The eraser is the most important piece of hardware
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is not well defined - at least to us. It's hard (impossible/a waste of time) to offer suggestions without knowing what you're really going for. Please define the problem more clearly, and we'll be happy to help.
 
Joanna Spence
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am building an anagram solver which takes an input word, and searches the dictionary based on the prefixes of the word..

It has to use recursion and backtracking and ignore spaces in between words.

I came up with a method for this, but not sure if it will work out correctly.

In the beginning of the program, i read in a dictionary list of words into dictionaryList, and the test file from the command line into testList

Then I used the stringtokenizer to create an ArrayList of all the letters contained in each word called letterList.

The letterList would be used to iterate through and create prefixes to search the dictionary on...

I would use the method I outlined below to search for anagrams..





 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elmira Love wrote:i figured out if i use the .replace() function with string.replace(" ","") it removes the spaces from in between the words, but now my anagram solver is having trouble finding matches because the string is all the words bunched together.

How would i ignore the space but still be able to successfully search for anagrams?


IMHO, the easiest solution is not to permanently remove the spaces. Keep working with the string that has the spaces, and hence, has the words. Only when you need to confirm whether it is an anagram to do make a new (and temporary) string, with the spaces removed, that you can check for anagrams. When you are done, throw away the string without the spaces -- and go back to the original string.

Henry

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic