DEV Community

Vadim Stakhnyuk
Vadim Stakhnyuk

Posted on

Reversing A String Using Pointers(References)

In my hunt for algorithm practice, I came across an algorithm that is pretty common known as reversing a string. There are many ways to reverse a string. These many ways commonly get filtered out based on the way either a problem description or an interviewer wants you to solve the problem. At first, not reading the directions, I dove straight into this problem. Having seen this problem before and having minimal knowledge off the top of my head, I began solving this problem by allocating more memory storage and creating a new array with which I can loop through the old array starting from the last element and going down to the first, and pushing all the elements from the string into the new array.

var reverseString = function(string) {
    let reversedString = [];
    for (let i = string.length - 1; i >= 0; i--) {
      reversedString.push(string[i])
    }
    return reversedString;
};

reverseString(["h", "e", "l", "l", "0"])

input = ["h", "e", "l", "l", "o"]
output = ["o", "l", "l", "e", "h"]

Having figured out the answer on a separate programming IDE, I went ahead and tried submitting the problem. At this point I was hit with a "Incorrect Answer" message. I quickly went back and reread the directions. This time I saw that I was not allowed to allocate memory by creating a new array.

At this point, I knew about the Javascript function .reverse() and how easy it would be to solve the problem with this helper function. Sure enough, I was able to complete the problem and the website was able to accept my answer.

var reverseString = function(string) {
    return string.reverse();
};

reverseString(["h", "e", "l", "l", "0"])

input = ["h", "e", "l", "l", "o"]
output = ["o", "l", "l", "e", "h"]

After looking at this problem, I imagined having to solve this algorithm problem in an interview scenario, which is definitely a possibility. Having this thought, I knew the possibility of using helper functions such as .reverse() may not always be accepted.

At this point, I cleared my IDE and starting going at this problem using pointers and modifying the origin array without creating a new one and not having to use helper methods such as .reverse().

I was able to use indexing and having pointers set to the first and last letters and then incrementing towards the middle of the array and swapping the letters.

var reverseString = function(string) {
    let left = 0;
    let right = string.length - 1;
    while (left < right) {
        let temp = string[left];
        string[left++] = string[right];
        string[right--] = temp;
    }
};

reverseString(["h", "e", "l", "l", "0"])

input = ["h", "e", "l", "l", "o"]
output = ["o", "l", "l", "e", "h"]

This was my little journey coming upon an algorithm and using a few of the many ways that you can solve this problem. Ultimately in the process, I learned about using pointers to help solve problems such as this one. Using pointers is a great way to reverse strings without allocating additional memory with a new array.

Top comments (0)