Reversing a String

This is historically the first most basic thing anyone needs* to do in computer programming. *By needs, I mean it is in no way useful in any circumstance.

The user says "I like that the words flow in one meaningful direction, but what I really need is to be able to turn them into something difficult for me to read, yet easy for enemy spies to decipher. That would certainly be useful to me!"

That being said, the (very basic) skills learned in doing so can be applied to more useful circumstances as long as you import the imagination and free-thinking libraries to your brain - required, if you're going to be writing useful code.

I'm always terrified I don't know enough about code. That drives me to learn more, but it also makes me underestimate and underrepresent myself. I think: "Reverse a string? How am I ever going to accomplish that?!" Well, with baby steps. Actually more like zygote steps. If zygotes step at all.

But before this analogy gets even more uncomfortable, let me just say that even if reversing a string sounds intimidating, there is one kind of string I can reverse... I can reverse any palindrome!

in JavaScript:

function reversePalindrome(string) {  
    return string;
}

console.log(reversePalindrome('racecar'));  

in Python:

def reversePalindrome(string):  
    return string

print reversePalindrome('racecar')  

in Java:

public class palindrome {  
    public static String reverse(String text) {
        return text;
    }

    public static void main(String [] args) {
        System.out.println(reverse("racecar"));
    }
}

The point here is that the nature of the challenge doesn't matter. It's the approach that does. By taking an iterative approach, you can tackle any challenge regardless of whether it's simple, challenging, or just requires a great amount of complexity. The first thing is to get something up and running, then add more functionality, each time delivering a running prototype.

You might notice, for example, that if we don't put in a palindrome, we won't get it's reverse. So obviously we'll need to write a helper function to check to see if it's a palindrome. If it is, the program continues, if not, it'll give us an excuse.

To do this, our helper function will simply have to reverse the string to check its value against the original. If they're the same, its a palindrome. we can then be certain our palindrome reversing function will only reverse palindromes! Behold! The power of iterative design! :P

//I am soooo ready for coding interviews now!

function check(string) {

    var rvrs = ''
    for ( i=string.length-1; i>=0; i-- ) {
    rvrs += string[i];
    }

    return rvrs;

}

function reversePalindrome(string) {

​    var excuse = 'Hey... ' + string + 
    ' isn\'t a palindrome. How am I supposed to reverse that?!';

​    if (string === check(string)) { return string; } 
    else                          { return excuse; }

}

Of course, Python has a super simple way to reverse a string.

def reverse(string):  
    return string[::-1]