• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a palindrome program - running into issues with arrays?  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a program that reads a sentence, such as: "abba is running to the radar" scans this sentence, and then prints out all the palindromes. I am running into issues with my arrays and for statements.
Here is my code:



I am aware that there is a few "useless" variables in there at the moment, I will clean them up (as well as some useless statements, I see those too). The issue comes at about line 17. The variable backwards REMOVES all the spaces from the array, so when it comes time to compare the strings, it is comparing individual words to the ENTIRE string, thus no words will ever be a palindrome.
I realise the problem, I just need a small hint on what I could to fix it? Thanks for any help.


 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After appending the word to backwards, you need to append a space as well, something like backwards += " ";
 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:After appending the word to backwards, you need to append a space as well, something like backwards += " ";


Will this append a space between every character? Is there a way to append a space into only one piece of the string?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will append a space between the words. Now to selectively append the space you would have to tell us more about when you need to append the space. You can try out a simple program on just appending strings and then once you are clear with that you can incorporate your findings in this large program.

Its good to isolate and solve the problem rather than solving it as part of another larger problem.

While you are appending the space between the words you need to make sure that you dont append the space after the last word, because you need spacing only between the words.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's go through what your program does.

1) It lower-cases the the input
2) It splits the input into a words array by a space
3) It creates a string with each word in reverse order with no spaces

Why do you do this step? If someone inputs "Now is the time" you get "timetheisnow". How is this useful?

4) You loop through the words comparing each word to this string

What do you really want to do? If you want to see if each individual word is a palindrome, then what would be a better way to check that?
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem looks over complicated to me.

1.
a. Check is all phrase is a palindrome (you can use charAt and loop to read in reverse way and compare using equalsIgnoreCase).

2.
a. Split string by " ", so you'll get array (you've done this).
b. Read each word in reverse way (can use one loop and charAt).
c. Compare with original word.


I don't see any other tricky things in this problem. Unless I don't understand the OP's problem.
Please correct me if i'm wrong.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aarden Axford, have you got your solution?

I discovered, that palindromes could be a few different types: original sentences with no changes, single words, and sentences with removed delimiters.
So, actually there are few scenarios you need to pay attention on.

Anyway, i got my solution. So what is yours?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!