Blog Post

Reversing a string without temp variables or library functions

🗓 August 23, 2012 :: 🕑 2 min read :: 👏 0 💬 0

So yesterday my friend Jimmy asked me to reverse a string while we were in one of the computers labs at uni. At first I thought “hey, isn’t this like really simple and straightforward, could just use a StringBuffer?“. Then Jimmy said “You can’t use any library functions or temporary variables”. I thought for a second and said “you can do it with arrays” and surely you can definitely do it with arrays as shown by one of our other friend Kevin.

But was that was messy and surely it wasn’t the only solutions. Like many programming problems, this isn’t!

I figured this can be done with a little bit of looping and string manipulation (Note: implementation is in Java). As shown in line 25-27, I looped through the string and reassembled it n times. The reassembling is actually quiet tricky because I reconstructed the original string by cutting and carefully rearranging it every loop. This solution is pretty messy and hard to comprehend without a pen and paper to follow through with it (well at least for me).

It was just past 10 o’clock and I looked at the clock on the top right of my MBP. The time was 10:01 pm, then all of a sudden an idea struck me, I could have just reverse the string using a reverse loop, append it to the original string and cut the palindromic string leaving only the latter half. This solution (line 33-36) was much simpler and easier to remember.

Can’t believe something so simple took me this long to think up. Thank you palindrome!

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class reverseString { public static void main (String[] args) { String s = null; int length = 0; System.out.print("Enter a string: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { s = br.readLine(); } catch (IOException ioe) { System.out.println("IO error"); System.exit(1); } length = s.length(); System.out.println("Initial String " + s); //Reverse for (int i = 0; i < length; i++){ s = (s.substring(1, s.length() - i) + s.charAt(0) + s.substring(s.length() - i)); } System.out.println("Reversed String " + s); //Reversing is back to original // -- Decided to be smart about it for (int i = (length - 1); i >= 0; i--){ s += s.charAt(i); } s = s.substring(length); System.out.println("Reverted String " + s); } }
Ben Shi

So yesterday my friend Jimmy asked me to reverse a string while we were in one of the computers labs at uni. At first I thought “hey, isn’t…

https://hbish.com/reversing-a-string-without-temp-variables-or-library-functions/


Fetching Replies...