Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Creating a palindrome program - running into issues with arrays?

 
Ranch Hand
Posts: 38
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • 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 += " ";
 
A Axford
Ranch Hand
Posts: 38
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • 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: 6268
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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?
 
Marshal
Posts: 7179
491
Mac OS X VI Editor BSD Linux
  • 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
Marshal
Posts: 7179
491
Mac OS X VI Editor BSD Linux
  • 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?
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!