Various Ways to Reverse a String in C#

This blog post provides some code examples that desciribes in C# how to reverse a string. As a part of the blog the followings are the discussion points.

  1. Reverse a String using For Loop
  2. Reverse a String using Foreach Loop
  3. Reverse a String using While Loop
  4. Reverse a String using Reverse function of Array

Various Ways to Reverse a String in C#

Reverse a String using For Loop

In the following C# program is written in console application and it takes a string as an input from the console. Then we reverse the string using for loop.

 class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.Write("Enter Your String : ");  
       string inputString = Console.ReadLine();  
       string resultString = string.Empty;  
       for (int i = inputString.Length - 1; i >= 0; i--)  
       {  
         resultString += inputString[i];  
       }  
       Console.WriteLine("The Reverse string is {0}:",resultString);  
       Console.ReadKey();  
     }  
   }  
Result
 Enter Your String: KAILASH' BLOGS  
 The Reverse string is : SGOLB 'HSALIAK  

Reverse a String using Foreach Loop

The following code snippet takes a string as input and reverse the string using foreach loop.

 static void Main(string[] args)  
     {  
       Console.Write("Enter Your String : ");  
       string inputString = Console.ReadLine();  
       string resultString = string.Empty;  
       foreach (char c in inputString)  
       {  
         resultString = c + resultString;  
       }  
       Console.WriteLine("The Reverse string is {0}:",resultString);  
       Console.ReadKey();  
     }  
Result
 Enter Your String : KAILASH'S BLOG CSHARP JOURNY  
 The Reverse string is : YNRUOJ PRAHSC GOLB S'HSALIAK  

Reverse a String using While Loop

The following code snippet takes a string as input and reversing the string using while loop.

 class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.Write("Enter Your String : ");  
       string inputString = Console.ReadLine();  
       string resultString = string.Empty;  
       int i = 0; ;  
       while (i < inputString.Length)  
       {  
         resultString += inputString[i];  
         i++;  
       }  
       Console.WriteLine("The Reverse string is {0}",resultString);  
       Console.ReadKey();  
     }  
   }  
Result
 Enter Your String : KAILASH'S BLOGS SHARES WHAT EXPERIENCED  
 The Reverse string is KAILASH'S BLOGS SHARES WHAT EXPERIENCED  

Reverse a String using Reverse Function of Array

The following code example, converts the provided string to a character array then reverses the elements (charachers in array) of the array using the reverse method . Once the elements of the character array reversed, the array is coverted again to a string.

 class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.Write("Enter Your String : ");  
       string inputString = Console.ReadLine();  
       string resultString = string.Empty;  
       char[] convertedArray = inputString.ToCharArray();  
       Array.Reverse(convertedArray);  
       resultString = new string(convertedArray);  
       Console.WriteLine("The Reverse string is {0}",resultString);  
       Console.ReadKey();  
     }  
   }  
Result
 Enter Your String: KAILASH' BLOGS  
 The Reverse string is : SGOLB 'HSALIAK  

Note:-

In the loop of above code snippets a string is manupalated to store the reverse characters every time from the input string. As we know string manupulation consumes lots of system resources becase string is immutable and creates instance of new string each time the string is manupulated, Hence it is recomended that use StringBulder insted of string to in the loop like below code example.

 class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.Write("Enter Your String : ");  
       string inputString = Console.ReadLine();  
       StringBuilder sb = new StringBuilder(inputString.Length);  
       for (int i = inputString.Length - 1; i >= 0; i--)  
       {  
         sb.Append(inputString[i]);  
       }  
       Console.WriteLine("The Reverse string is {0}", sb.ToString());  
       Console.ReadKey();  
     }  
   }  
Summary

In this blog post, we explored various methods that help to reverse a string in C#. I hope this post is helpful to you.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال