Reverse a String Using for Loop

Let us proceed and create a sample program to understand the logic to implement the same.

Write the below code in a console application.

  1. static void Main(string[] args)  
  2. {  
  3.     string _Inputstr = string.Empty, _Reversestr = string.Empty;  
  4.     Console.Write("Enter the string : ");  
  5.     _Inputstr = Console.ReadLine();  
  6.     for (inti = _Inputstr.Length - 1; i >= 0; i--)  
  7.     {  
  8.         _Reversestr += _Inputstr[i];  
  9.     }  
  10.     Console.WriteLine("The reverse string is {0}", _Reversestr);  
  11.     Console.ReadLine();  
  12. }  
Take two strings. One as the input string that accepts a string from the user, and the second string is used to store the string in reverse order. For loop is used to traverse the string length and append each character to the reverse string.

Output

 

Ebook Download
View all
Learn
View all