Immutability and how to Reverse Strings

Immutability and how to Reverse Strings

In this article I will be writing about how to reverse a string in multiple programming languages, the method, function or operator to reverse a string is available to programmers through programming language through libraries. So what does it mean to reverse a string, it can be described by an example where a string Hello, welcome to my blog is changed to golb ym ot emoclew ,olleH. To humans it can be very easy to re-write this string in reverse but with computer programming languages this task has its intricacies. A common intricacy that will be discussed in this article is that of Immutability. Immutability as a property of strings in most languages means programmers learn to work with the limitations of this language design to solve string related problems.

Javascript string reverse example

// Create a string object
let welcome_str = "Hello, welcome to my blog";
// Print to console a reversed string
console.log(welcome_str.split('').reverse().join(''));

String Objects are immutable in Javascript, meaning we cannot change the order/state of a string Object after instantiation. From the above the split method turns a string into an array. Arrays are mutable in JS and this property makes arrays a suitable object to turn strings to before reversing. split stores each character of the string in an array while join converts an array of reversed content back to a string. The quotes in split means that each character in the string should be removed for storage in an array. With join each character in array must be recombined to form a string, note the string has a couple of white spaces in between, these are also characters.

Python string reverse example

// create a string object
welcome_str = "Hello, welcome to my blog"
// print to console the reversed string
print(welcome_str[::-1])

The python slice operator is represented by [start_index : end_index : skip], this operator can be compared to the python function range which also has 3 parameters start_index, end_index, skip. In the code snippet above the first 2 parameters have the default index of 0 and the string's size respectively while the skip parameter has a value of -1. When this operator is applied as shown in the code snippet, the string Hello, welcome to my blog stored in variable welcome_str is reversed. The slice operator does more than just reverse strings, to learn more visit here.

Java string reverse example

// create a mutable string object using StringBuilder class
StringBuilder str = new StringBuilder("Hello, welcome to my blog");
// print to a new line in console the reversed string
System.out.println(str.reverse().toString());

The method toString returns a string, in Java the string Object is immutable. The method reverse changes the string as described in the introduction. Instead of creating string Hello, welcome to my blog using an object of class String, the class StringBuilder was used. The later allows for strings to be mutated.

Key Takeaways

It is fair to say that whenever solutions are implemented to change the order/state of strings in most languages immutability plays a part in the way a programmer implements his solution. Although not very obvious python strings are Immutable. With Javascript we convert immutable strings to arrays, while we had to create strings as an object of StringBuilder in Java although python did neither in the background a new object is created to represent that the state/order of a string has changed. To add python also has the shortest implementation when compared to the other 2 implementations. To run either of the 3 implementations above you can make use of the online IDE replit.