Reverse a string

Return the string s backwards. Try it with a loop first, without a built-in reverse.

Write reverse_string(s) in Python, reverseString(s) in JavaScript or TypeScript, or void reverse_string(char s[]) in C. Level: Easy.

Examples

Input: s = 'hello'
Output: 'olleh'
Input: s = 'Python'
Output: 'nohtyP'
  • 0 ≤ length of s ≤ 1000

Hint

Start with an empty result and put each character in FRONT of it: result = ch + result.

The approach, step by step

  1. result = ""
  2. Characters left in s?
  3. ch = next character
  4. result = ch + result
  5. Return result

← All practice problems

Try AI Flowchart free →