How to reverse a String without using length
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The following constructs and similar are not allowed:-The following are permitted however:-I knocked something up in ¼ hour last night which takes Strings as command‑line arguments and prints them backwards. There appear to be at least two ways to do it. Let's see what people can find.
On your marks,
Get set,
GO!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I like the first better because it does not rely on Exception handling for flow control.
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
), I shall be all right.I ahd to add some ‍ characters so :) didn't appear asjava WordReverser "Read JavaRanch :)" ieuhfblk837
“Read JavaRanch :)” reversed is “): hcnaRavaJ daeR”
“ieuhfblk837” reversed is “738klbfhuei”

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
OK. That's my lot.
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Shows how badly the original Stack class was designed that you didn't use a class called Stack or XXXStack or StackXXX. Shouldn't you use ArrayDeque rather than a linked list?Matthew Brown wrote: . . . stick everything on a stack and pop it off again. . . .
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Matthew Brown wrote:How do we feel about things that probably use the length property under the hood
Probably hard to avoid it. Even Campbell and I's use of string.substring(1) almost certainly calls string.substring(1, string.length()) under the hood too. I have no idea what CharArrayReader does behind the scenes but I imagine it knows the length of it's current buffer which may or may not be the full length of the character array.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Unfortunately using .equals() doesn't get us off the hook either.
(Taken from my JDK 1.7 source.)
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Cooke wrote:I have no idea what CharArrayReader does behind the scenes
Dammit!!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Campbell Ritchie wrote:
Shows how badly the original Stack class was designed that you didn't use a class called Stack or XXXStack or StackXXX. Shouldn't you use ArrayDeque rather than a linked list?Matthew Brown wrote: . . . stick everything on a stack and pop it off again. . . .
ArrayDeque would have been fine (and may be slightly faster), but they both do the job well.
Obviously, there should be an interface called Stack (instead of the legacy class that extends Vector - I assume part of the problem is that they didn't want to remove that and so what do you call it?), rather than the monolithic Deque interface. If they had that, it doesn't really matter whether you have an XXXStack class that implements it or you let ArrayDeque and LinkedList implement it and use those.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Cooke wrote:A variation on the last one. If the Java compiler ever supports tail recursion optimisation then this one would win out over the last as it would occupy constant stack space. Although String concatenation is inefficient over using the StringBuilder which I used in one of my other solutions.
Interesting (or not) fact: one thing about these approaches that use substring() is that they're much more efficient using Java 6 than Java 7(u6+). It used to reuse the underlying array and use an offset, so substring() was constant time. But that was changed to prevent memory leaks when you take the substring of a very large String...with the drawback that it's now a linear time operation.
http://java.dzone.com/articles/changes-stringsubstring-java-7
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That has been discussed here before, as the only sensible justification for new String("rhubarbrhubarb");Matthew Brown wrote: . . . that was changed to prevent memory leaks when you take the substring of a very large String. . . .
Even that has gone.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
After getting the string's length, the number of possibilities to reverse the string is countless.
Claudiu
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Edit: Looking back through the topic since I left it and hit post reply, it would seem that my use of `isEmpty()` is not allowed either.
"Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
String c = "WhateverReveTAHw";
boolean isPalindrome = c.toLowerCase().equals(new Stringbuilder(c).reverse().toString.toLowerCase());
Greetz,
Piet
There are three kinds of actuaries: those who can count, and those who can't.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Piet Souris wrote:Needed it a couple of weeks ago in ProjectEuler, testing for palindromes:
String c = "WhateverReveTAHw";
boolean isPalindrome = c.toLowerCase().equals(new Stringbuilder(c).reverse() .toString.toLowerCase());
Greetz,
Piet
Whilst it certainly does the job well, it doesn't meet the condition to not use the reverse() method of String.
"Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
. Great work, one of the very interesting threads!-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Matthew Brown wrote:Interesting (or not) fact: one thing about these approaches that use substring() is that they're much more efficient using Java 6 than Java 7(u6+)
That is interesting. I did not know that.
Tim Driven Development | Test until the fear goes away
| I need a new interior decorator. This tiny ad just painted every room in my house purple. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











