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.
- Reverse a String using For Loop
- Reverse a String using Foreach Loop
- Reverse a String using While Loop
- 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