DEV Community

loading...
Cover image for Examples for reversing a string in JavaScript, Python and Dart

Examples for reversing a string in JavaScript, Python and Dart

andrewbaisden profile image Andrew Baisden ・2 min read

One of the most common interview questions is how do you reverse a string in the programming language that you know. Here are a few examples of how easily it can be done. As these programming languages are C based the syntax is quite similar so it is easy to translate across languages.

Example 1

JavaScript

function reverse(str) {
        console.log(str.split('').reverse().join(''));
}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
  print(''.join(reversed(str)));


reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    print(str.split('').reversed.join(''));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Example 2

JavaScript

function reverse(str) {
    reversed = '';

    for (let char of str) {
        reversed = char + reversed;
    }
     console.log(reversed);

}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
  reversed = '';
  for char in str:
    reversed = char + reversed;

  print(reversed);


reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    List newStrings = [];
    dynamic index = (str.length);

    while (index > 0) {
      index -= 1;
      newStrings.add(str[index]);
    }
       print(newStrings.join(""));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Example 3

JavaScript

function reverse(str) {

    const arr = [];
    for (let i = str.length - 1; i > -1; i--) {
        arr.push(str[i]);
    }
    console.log(arr.join(''));
}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
    new_strings = []
    index = len(str)
    while index:
        index -= 1                       
        new_strings.append(str[index])
    print(''.join(new_strings))

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    List arr = [];
    for (var i = str.length - 1; i > -1; i--) {
      arr.add((str[i]));
    }
    print(arr.join(''));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Example 4

JavaScript

function reverse(str) {

    const arr = [];
    for (let i = str.length - 1; i > -1; i--) {
        arr.push(str[i]);
    }

  // Using regex
    console.log(arr.toString().replace(/,/g, ''));

  // Using reduce
    console.log(arr.reduce((acc, cur) => acc + cur));

}

reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Python

def reverse(str):
  print(str[::-1]);


reverse('Hello World');
Enter fullscreen mode Exit fullscreen mode

Dart

void main() {
  reverse(str) {
    List arr = [];
    for (var i = str.length - 1; i > -1; i--) {
      arr.add((str[i]));
    }

    // Using Reduce
    print(arr.reduce((acc, curr) => acc + curr));
  }

  reverse('Hello World');
}
Enter fullscreen mode Exit fullscreen mode

Discussion (4)

pic
Editor guide
Collapse
ericchapman profile image
Eric Chapman

In Ruby....

"Hello World".reverse

I love Ruby :-)

Collapse
ra1nbow1 profile image
Matvey Romanov

Really modern way to solve this problem

Collapse
shajib126 profile image
Abu zubaer

how did you use the image inside of write?

Collapse
andrewbaisden profile image
Andrew Baisden Author

If you are referring to the code examples in the post then it's a markdown code block. If you mean the cover image then it's just photoshop.