• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reverse a string in-place

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can this be done in Java?
 
Ranch Hand
Posts: 143
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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™.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed - you can even use StringBuilder.reverse() to achieve this.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic