aspose file tools*
The moose likes Beginning Java and the fly likes reverse a string in-place Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reverse a string in-place" Watch "reverse a string in-place" New topic
Author

reverse a string in-place

C Halbe
Greenhorn

Joined: Jul 18, 2008
Posts: 20
Can this be done in Java?
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Not sure what you mean by "in place". If you mean without creating any new references in memory, then no as Strings are not mutable.
C Halbe
Greenhorn

Joined: Jul 18, 2008
Posts: 20
Hi Brian,

Thanks for replying.

Yes, that's what I meant by "in-place". So I guess, if someone asks me how to reverse a string "in-place" I will have no choice but to implement it in C/C++, because in Java, while returning the reverse String we will create a new String.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Right. On the other hand, if a "string" (without capitalization) can be represented as a char[] array, then you can certainly reverse that in place, pretty much the same as you can in C/C++. It's just that in Java that's not the standard way to represent a String.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

C Halbe wrote:So I guess, if someone asks me how to reverse a string "in-place" I will have no choice but to implement it in C/C++


Your other choice is to tell them to take a hike, as there's no good reason to ask for that, other than as an exercise when teaching some feature of the language or API, and since Java doesn't have that feature (at last not for String with a capital "S"), there's no point at all in asking it in Java.
C Halbe
Greenhorn

Joined: Jul 18, 2008
Posts: 20
Jeff Verdegan wrote:
Your other choice is to tell them to take a hike.


LOL....I was just asking this because this is asked in interviews. If asked, I will have to show the implementation in other language, not Java.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

C Halbe wrote:
Jeff Verdegan wrote:
Your other choice is to tell them to take a hike.


LOL....I was just asking this because this is asked in interviews. If asked, I will have to show the implementation in other language, not Java.



Personally, if this is asked in an interview, I wouldn't just give the "it can't be done in Java" response. I would kindly mention that strings are immutable and asked for more clarification on the question.

I would also ask to see if they are looking for a way to reverse a char array, without using temporary variables. And...

1. if they are looking to reverse an array,without another array, talk about swapping the elements -- using two indexes, one forward, one backwards. (or using one index variable)

2. If they don't even want a temporary char variable, then talk about using bit wise logic to swap the two elements. etc.

Basically, brainstorm. The goal is to show the interviewer your thinking process.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Reversing a string in place is a common exercise for C/C++ programmers. It is quite possible the interviewer didn’t know it’s impossible in Java™.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

If you'd have a StringBuilder or StringBuffer, then you it would be able to reverse the content of that in-place.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Indeed - you can even use StringBuilder.reverse() to achieve this.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Mike Simmons wrote:Indeed - you can even use StringBuilder.reverse() to achieve this.

That wouldn't be much of an exercise.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

Are they asking you to reverse a String in place, or a string in place? These are very different questions, and so my first questions would be to clarify what exactly they mean.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Dennis Deems wrote:
Mike Simmons wrote:Indeed - you can even use StringBuilder.reverse() to achieve this.

That wouldn't be much of an exercise.

Sure, which is why I originally suggested considering a char[] array instead - as noted above, it's a standard C/C++ interview question, and if they want to see you do some actual coding, fine. But if they do allow you to use a StringBuilder or StringBuffer, then of course my first answer would be to simply use the already-existing library method. If that's not what they want, then that can be addressed in subsequent conversation. But there's no need to bypass the easiest and most obvious solution.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
I think only the C method with pointers is the only method which counts as “in place”; all the other suggestions involve moving the String somewhere else and reversing it there.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
The other solutions were assuming that a "string" is not necessarily a String, in which case it may not need to be moved anywhere. If it's a char[] array to begin with, you can keep using that same char[] array, no other objects.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
It’s easy enough to reverse a char[] in place; in fact, in C, what they call a string is really a *char or a char array. But remember in Java™ “An Array of Characters is not a String”.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Campbell Ritchie wrote:But remember in Java™ “An Array of Characters is not a String”.

Yes, that point has been made, repeatedly - it's implicit in any comment that differentiates between a string and a String. But we don't know the actual intent of the original questioner (string or String?); we're just guessing and discussing possibilities.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: reverse a string in-place