Python 🐍

s = "Hello World"
reversed = s[::-1]

Most people know slice syntax as [start:end], but this isn't the full form. A third, optional, parameter could be given, called the stride. If the stride is negative, then the striding starts from the end of the string. In this code, the string is being strided by 1, but starting from the end of the string, as the stride is negative, resulting in a reversed string.

JavaScript 💻

var s = "Hello World";
var reversed = s.split("").reverse().join("");

This code is straightforward. The string gets split, by character, into a list. The list is then reversed, and a string is reconstructed.

Java 🖱️

public class Main {
    public static void main(String[] args) {
        String s = "Hello World";
        String reversed = new StringBuilder(s).reverse().toString();
    }
}

This code uses the built-in StringBuilder. A StringBuilder object is created, reversed, then converted back to a string.

C++ 🐱‍💻

#include <string>
int main() {
    std::string s("Hello World");
    std::string reversed(s.rbegin(), s.rend());
}

This code starts off by including the string library. Then, the string's reverse iterators are used to create a new string.

Share this project:

Updates