- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
There are lots of ways to reverse a string. As developers develop a new logic, they can build new algorithms.
In this tutorial, we will see some most popular algorithms and methods to reverse a string by creating custom functions and built-in methods.
Using the reverse() Method
As we are using the built-in reverse method, it is an optimized method because every library function is created in such a way that it optimizes the better time complexity and space complexity.
Users can follow the below algorithm to reverse strings using the reverse() method.
Step 1 − Convert the string to the array of characters using the spread operator. The spread operator is the three dots (...), which is used to clone any object or array.
Step 2 − Next, reverse the array of characters using the reverse() method.
Step 3 − Join the reversed character array using the join() method, and your reversed string is ready.
Users can follow the syntax below to use the algorithm's methods.
let string = "TutorialsPoint"; let charArray = [ ...string ]; // convert string to char array charArray.reverse( ); // reverse the array of character string = charArray.join(''); // join array of character.
The below example demonstrates the use of the above algorithm.
In the above output, users can see that our string is reversed finally.
Creating the Custom Algorithm using for Loop
This approach will create an incrementing for loop to reverse a string. We will reverse a string in-place in this section. The in-place reverse means to reverse the string without using the extra space or new variable. In the above approach, we have created the array of char, which means we have used the extra space.
Users can follow the below algorithm to reverse a string using the for a loop.
Step 1 − Iterate through the string from start to end of the string. We can find the endpoint of the string using the string length.
Step 2 − Start to append every character of the string in the front of the new string.
Step 3 − Continue step 3 till the end point of the string, and you will get the reversed string.
Users can follow the syntax below to reverse a string using the loop.
In the below example, we have used the reverseString variable to store the reverse string and using the for loop we are making the string reverse.
Creating a Recursive Function to Reverse a String
This section will teach the recursive approach to reverse a string. We will not use any extra variables in this approach like the above approach, but when we make a recursive call, it stores all function calls and stack traces, which take more space than the single variable.
Follow the below algorithm to reverse a string recursively.
Step 1 − If the string is empty, return ‘ ’ which is an empty string.
Step 2 − If the string is not empty, append the first character at last and make a recursive function call sub-string starting from the first character to the end of the string.
Step 3 − Return the returned value from the recursive call of sub-string by appending first character of string at last.
Users can follow the below syntax to implement above algorithm.
In the below example, we have implemented the recursive approach to reverse a string.
In this tutorial, we have learned three different approaches to reversing a string. The first approach is the easiest and requires two lines of code as we use the built-in reverse method. The second approach is the best as it takes the lowest memory. The third approach is not so good as it will take more time and space.
- How do you reverse a string in place in C or C++?
- How do I modify a string in place in Python?
- How to do reverse of string in Android?
- How to reverse a string in Python?
- How do you make a string in PHP with a backslash in it?
- Reverse a string in C#
- How to quickly reverse a string in C++?
- How to reverse a given string in Java?
- How can I reverse a string in Java?