• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reverse a string in-place

 
C Halbe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can this be done in Java?
 
Brian Burress
Ranch Hand
Posts: 130
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you'd have a StringBuilder or StringBuffer, then you it would be able to reverse the content of that in-place.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed - you can even use StringBuilder.reverse() to achieve this.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic