• Post Reply Bookmark Topic Watch Topic
  • New Topic

String reverse  RSS feed

 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Is there any way how to reverse a string value without using reverse(),length()?


Thanks,
D.Srikanth
 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there is.

Any particular reason you don't want to use reverse() or length()? Homework assignment perhaps?

Have you had any ideas yourself on how you'd do this? If so please share and let us know what part you're having trouble with.
 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys got the solution have used toCharArray and found the length and iterated..

Tim Cooke wrote:Yes there is.

Any particular reason you don't want to use reverse() or length()? Homework assignment perhaps?

Have you had any ideas yourself on how you'd do this? If so please share and let us know what part you're having trouble with.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I challenge you to reverse a String without knowing its length. I don't think it is easy without knowing lengths.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I challenge you to reverse a String without knowing its length. I don't think it is easy without knowing lengths.

Is the question to reverse it without knowing it's length, or to reverse it without using the length() method?

I posit you can do it without using the method - although you still have to find the length.
 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I challenge you to reverse a String without knowing its length. I don't think it is easy without knowing lengths.

Challenge accepted!

I have a solution but I'll hold off and let the OP have a go first.

Edit: If the OP wants to of course
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant without knowing its length.
If you use the length() method, it simply returns the length of the underlying array. So using toCharArray().length hardly counts as different from the length() method.

I think I can probably work out how to do it, too. Agree with Tim Cooke let's see whether OP can rise to the challenge!
 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi campbell have tried can you please tell me what is that other way ,want to know it...

Campbell Ritchie wrote:I meant without knowing its length.
If you use the length() method, it simply returns the length of the underlying array. So using toCharArray().length hardly counts as different from the length() method.

I think I can probably work out how to do it, too. Agree with Tim Cooke let's see whether OP can rise to the challenge!
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth,

If i may, there are two obvious ways of attacking this - without prying into the class and/or converting to an array.

My suggestion, read the String object javadocs (very, very carefully) or you can do a search for similar threads on these fora.


-steve
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two completely different ways. But I'm sure that if you turned a group of programmers (who like to show off) loose they would come up with at least a dozen ways.
 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth darbha wrote:have tried can you please tell me what is that other way

What have you tried? What ideas / approaches did you think might work?

Steve Fahlbusch wrote:read the String object javadocs (very, very carefully)

I think I know where you're going with this. That would work too.

So now thanks to Steve I also have two different ways of doing it. However I don't like one of them because it uses Exception handling for flow control.
 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
The only way which i understood is to call the toCharArray and iterating it,i have told this before i am not having idea so if possible tell me the solution rather than just increasing the posts.. with discussions and cross questioning..

Tim Cooke wrote:
srikanth darbha wrote:have tried can you please tell me what is that other way

What have you tried? What ideas / approaches did you think might work?

Steve Fahlbusch wrote:read the String object javadocs (very, very carefully)

I think I know where you're going with this. That would work too.

So now thanks to Steve I also have two different ways of doing it. However I don't like one of them because it uses Exception handling for flow control.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knocked something up last night which uses 7 lines of code to reverse a pre‑existing String without using the length() method directly or indirectly and doesn't have any Exceptions.

I think OP has had long enough to respond, so let's post any suggestions here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!