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
- result = ""
- Characters left in s?
- ch = next character
- result = ch + result
- Return result