• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ignoring space in an anagram solver

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..





 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic