Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript Reverse a string in place

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. There are following methods to do so.

Methods to Reverse a String in Place:

Method 1: Using the JavaScript split() method

JavaScript String split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument.

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

Javascript




let str = "This is GeeksForGeeks";
console.log("Input string is : " + str);
 
function gfg_Run() {
    console.log(
        "Reverse String" +
        str.split("").reverse().join("")
    );
}
 
gfg_Run();


Output

Input string is : This is GeeksForGeeks
Reverse StringskeeGroFskeeG si sihT

Method 2: Using the Array reverse() method

JavaScript Array.reverse() Method is used for the in-place reversal of the array. The first element of the array becomes the last element and vice versa.

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

Javascript




// Input sting
let str = "This is GeeksForGeeks";
console.log("Input string is : " + str);
 
// Function to reverse string
function reverse(s) {
    if (s.length < 2) return s;
    let hIndex = Math.ceil(s.length / 2);
    return (
        reverse(s.substr(hIndex)) +
        reverse(s.substr(0, hIndex))
    );
}
 
// Function to display output
function gfg_Run() {
    console.log(reverse(str));
}
 
// Function call
gfg_Run();


Output

Input string is : This is GeeksForGeeks
skeeGroFskeeG si sihT

Method 3: Using the Array join() method

The JavaScript Array join() Method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(, ).

Example: This example takes a variable and appends the result from the end of the string. 

Javascript




// Input sting
let str = "A Computer Science Portal";
console.log("Input string is : " + str);
 
// Function to reverse string
function reverse(s) {
    // Variable to store reverse
    let o = "";
    for (let i = s.length - 1; i >= 0; o += s[i--]) {}
    return o;
}
// Function to display output
function gfg_Run() {
    console.log(reverse(str));
}
 
// Function call
gfg_Run();


Output

Input string is : A Computer Science Portal
latroP ecneicS retupmoC A

Method 4: Using substr() method

JavaScript str.substr() method returns the specified number of characters from the specified index from the given string. It basically extracts a part of the original string.

Example: This example uses str.substr() method to reverse a string.

Javascript




// Input sting
let str = "This is GeeksForGeeks";
console.log("Input string is : " + str);
 
// Function to reverse string
function reverse(s) {
    if (s.length < 2) return s;
    let hIndex = Math.ceil(s.length / 2);
    return (
        reverse(s.substr(hIndex)) +
        reverse(s.substr(0, hIndex))
    );
}
// Function to display output
function gfg_Run() {
    console.log(reverse(str));
}
 
// Function call
gfg_Run();


Output

Input string is : This is GeeksForGeeks
skeeGroFskeeG si sihT

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments