Reversing a String: Java vs JavaScript vs Python

py

Reversing a String can be done in multiple ways, using functions, loops. Let us compare how it is done in Java, JavaScript and Python.

Input: Hello, How are You?

Output: ?uoY era woH ,olleH

Java

public class ReverseString {
public static void main(String[] args)
{
System.out.println(new StringBuilder("Hello, How are You?").reverse().toString()); }
}

JavaScript

function reverseString(str) {
return str.split("").reverse().join("");
}
reverseString("Hello, How are You?");

Python

> 'Hello, How are You?'[::-1]
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s