Open In App

C# Program to Reverse a String without using Reverse() Method

Last Updated : 16 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

C# has a built-in function to reverse a string. First, the string is converted to a character array by using ToCharArray() then by using the Reverse() the character array will be reversed, But in this article, we will understand how to Reverse a String without using Reverse().

Example

Input  : Geek
Output : keeG

Input  : For
Output : roF

Method 1: Using a for loop to reverse a string.

An empty string is declared and name as ReversedString. The input string will be iterated from right to left and each character is appended to the ReversedString. By the end of the iteration, the ReversedString will have the reversed string stored in it.

C#




// C# program to reverse a string using a for loop
using System;
 
class GFG{
     
public static string Reverse(string Input) 
     
    // Converting string to character array
    char[] charArray = Input.ToCharArray(); 
     
    // Declaring an empty string
    string reversedString = String.Empty; 
     
    // Iterating the each character from right to left
    for(int i = charArray.Length - 1; i > -1; i--) 
    
         
        // Append each character to the reversedstring.
        reversedString += charArray[i]; 
    }
     
    // Return the reversed string.
    return reversedString;
 
// Driver code
static void Main(string[] args) 
    Console.WriteLine(Reverse("GeeksForGeeks")); 
}


Output

skeeGroFskeeG

Method 2: Using a while loop to reverse a string.

In this method, an empty string is declared and name as reversedString now the input string will be iterated from right to left using the while loop, and each character is appended to the reversedString. By the end of the iteration, the reversedString will have the reversed string stored in it.

C#




// C# program to reverse a string using while loop
using System;
 
class GFG{
 
public static string Reverse(string Input) 
     
    // Converting string to character array
    char[] charArray = Input.ToCharArray(); 
     
    // Declaring an empty string
    string reversedString = String.Empty; 
     
    int length, index; 
    length = charArray.Length - 1;
    index = length;
     
    // Iterating the each character from right to left 
    while (index > -1) 
    
         
        // Appending character to the reversedstring.
        reversedString = reversedString + charArray[index]; 
        index--; 
    }
     
    // Return the reversed string.
    return reversedString;
 
// Driver code
static void Main(string[] args) 
    Console.WriteLine(Reverse("GeeksForGeeks")); 
}


Output

skeeGroFskeeG


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads