JavaScript | Reverse a string in place.

In order to reverse the string in its place, We’re going to use a number of methods and the most preferred ones.
We’re going to use these methods(click to know more).

Example-1: This example reverses the string by first splitting it with (“”) separator and then reversing it and finally joining it by (“”) separator.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript |
      Reverse a string in place.
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 16px; font-weight: bold;">
    </p>
    <button onclick="gfg_Run()">
        Inplace Reverse
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 20px; 
              font-weight: bold;">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var str = 'This is GeeksForGeeks';
        el_up.innerHTML = "String = " + "'" + str + "'";
  
        function gfg_Run() {
            el_down.innerHTML = str.split("").reverse().join("");
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example-2: This example uses the concept of merge sort algorithm.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | Reverse a string in place.
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP"
       style="font-size: 16px;
              font-weight: bold;">
    </p>
    <button onclick="gfg_Run()">
        Inplace Reverse
    </button>
    <p id="GFG_DOWN" 
       style="color:green; font-size: 20px;
              font-weight: bold;">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var str = 'This is GeeksForGeeks';
        el_up.innerHTML = "String = " + "'" + str + "'";
  
        function reverse(s) {
            if (s.length < 2)
                return s;
            var hIndex = Math.ceil(s.length / 2);
            return reverse(s.substr(hIndex)) +
                reverse(s.substr(0, hIndex));
        }
  
        function gfg_Run() {
            el_down.innerHTML = reverse(str);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example-3:This example takes a variable and appending the result from the end of the string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | Reverse a string in place.
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 16px; 
              font-weight: bold;">
    </p>
    <button onclick="gfg_Run()">
        Inplace Reverse
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 20px; 
              font-weight: bold;">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var str = 'A Computer Science Portal'
        el_up.innerHTML = "String = " + "'" + str + "'";
  
        function reverse(s {
            for(var i = s.length - 1, o = ''; i >= 0; o += s[i--])
                  {}
                return o;
            }
  
            function gfg_Run() {
                el_down.innerHTML = reverse(str);
            }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.