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

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



Reffered: https://www.geeksforgeeks.org


C#

Related
C# Program to Find the IP Address of the Machine C# Program to Find the IP Address of the Machine
C# Program to Find Binary Equivalent of an Integer using Recursion C# Program to Find Binary Equivalent of an Integer using Recursion
C# Program to Print the Employees Whose Salary is Between 6000 and 8000 Using LINQ C# Program to Print the Employees Whose Salary is Between 6000 and 8000 Using LINQ
C# Program to Print the Numbers Greater Than 786 in an Integer Array using LINQ C# Program to Print the Numbers Greater Than 786 in an Integer Array using LINQ
C# Program to Print the Employees Whose ID is Greater Than 101 Using LINQ C# Program to Print the Employees Whose ID is Greater Than 101 Using LINQ

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10