Exercise 10: Reverse String (preserve spaces)

Problem Statement

Function `reverseString(str)` banao jo string ko reverse kare but spaces ko preserve kare. Example: reverseString("hello world") => "dlrow olleh"

Sample Output:

reverseString("hello") => "olleh"
reverseString("hello world") => "dlrow olleh"

Solution

const reverseString = (str) => String(str ?? "").split("").reverse().join("");

Explanation

Overall Goal:

  • String ko completely reverse karna.

Real world:

  • Text manipulation: reverse strings.
  • Algorithms: string reversal.