Reverse String using a Loop
- Initialize an empty string to store the reversed result.
- Iterate through the original string in reverse order.
- Append each character to the new string.
- The new string is the reversed version of the original string.
Below is the implementation to reverse a string using loop in different programming languages:
C++
#include <iostream> #include <string> std::string reverseStringLoop( const std::string& inputStr) { std::string reversedStr; for ( int i = inputStr.length() - 1; i >= 0; i--) { reversedStr += inputStr[i]; } return reversedStr; } int main() { std::string originalStr = "w3wiki" ; std::string result = reverseStringLoop(originalStr); std::cout << result << std::endl; return 0; } |
C
#include <stdio.h> #include <string.h> void reverseStringLoop( char inputStr[]) { int length = strlen (inputStr); for ( int i = length - 1; i >= 0; i--) { printf ( "%c" , inputStr[i]); } printf ( "\n" ); } int main() { char originalStr[] = "w3wiki" ; reverseStringLoop(originalStr); return 0; } |
Java
public class ReverseStringLoop { public static String reverseStringLoop(String inputStr) { StringBuilder reversedStr = new StringBuilder(); for ( int i = inputStr.length() - 1 ; i >= 0 ; i--) { reversedStr.append(inputStr.charAt(i)); } return reversedStr.toString(); } public static void main(String[] args) { String originalStr = "w3wiki" ; String result = reverseStringLoop(originalStr); System.out.println(result); } } |
Python3
def reverse_string_loop(input_str): reversed_str = "" for char in input_str[:: - 1 ]: reversed_str + = char return reversed_str # Example usage: original_str = "w3wiki" result = reverse_string_loop(original_str) print (result) |
C#
using System; class Program { static string ReverseStringLoop( string inputStr) { char [] charArray = inputStr.ToCharArray(); Array.Reverse(charArray); return new string (charArray); } static void Main() { string originalStr = "w3wiki" ; string result = ReverseStringLoop(originalStr); Console.WriteLine(result); } } |
Javascript
function reverseStringLoop(inputStr) { let reversedStr = "" ; for (let i = inputStr.length - 1; i >= 0; i--) { reversedStr += inputStr[i]; } return reversedStr; } // Example usage: let originalStr = "w3wiki" ; let result = reverseStringLoop(originalStr); console.log(result); |
Output
skeeGrofskeeG
Time Complexity: O(n)
Auxiliary Space: O(n) for storing the reversed string.
String Reverse in C/C++/Java/Python/JavaScript
String reverse or reverse a string means changing the position of each character of the given string to its opposite position from end, i.e. if a character is at position 1 then its new position will be String.length, similarly if a character is at position 2 then its new position will be String.length – 1, and so on.
Given a string, write a program to reverse the string.
Input: original_string[] = Geeks
Output: string_reversed[] = skeeGInput: original_string[] = w3wiki
Output: string_reversed[] = skeeGroFskeeG
Table of Content
- Reverse String using a Loop:
- Reverse String using inbuilt method
- Reverse String using Recursion:
- Reverse String using two pointers:
- String Reverse String using Stack: