Do you want to know how to write a C# program to reverse a string? In this C# tutorial, I will explain how to reverse a string in C#.
To reverse a string in C#, you can use a straightforward method involving a for loop or the built-in Array.Reverse method. For example, if you have a string “Hello”, you can convert it to a char array, apply Array.Reverse, and then reconvert it to a string, resulting in the reversed string “olleH”. This approach is efficient and easy to implement in any C# program.
Write A C# Program To Reverse A String
Reversing a string in C# means arranging its characters in the opposite order. For example, if our input is “Hello”, the reversed string will be “olleH”.
C# offers multiple ways to reverse a string. I’ll guide you through a simple and effective method using a for loop.
Here is a complete program to reverse a string in C#.
using System;
namespace StringReversal
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a string to reverse:");
string originalString = Console.ReadLine();
string reversedString = ReverseString(originalString);
Console.WriteLine($"Reversed String: {reversedString}");
}
static string ReverseString(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
}
- string originalString = Console.ReadLine();: This line takes the user’s input.
- string reversedString = ReverseString(originalString);: Here, we’re calling the ReverseString method and passing the user’s input to it.
- ReverseString Method: This method converts the string into a character array, reverses it, and then converts it back to a string.
After writing the C# code and when I run the program using the Visual Studio console application, it will prompt me to enter a string. Once entered, the program will display the reversed string by using the above C# code.
Here is the screenshot below:
There are also other methods to reverse a string in C#. Here are a few:
Reverse a string in C# using StringBuilder
Here is a complete program to reverse a string in C# using StringBuilder.
using System;
using System.Text;
class Program
{
static void Main()
{
string original = "Example";
string reversed = ReverseStringWithStringBuilder(original);
Console.WriteLine(reversed); // Output: "elpmaxE"
}
static string ReverseStringWithStringBuilder(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = str.Length - 1; i >= 0; i--)
{
sb.Append(str[i]);
}
return sb.ToString();
}
}
Write a C# program to reverse a string using LINQ
You can also use LINQ in C# to reverse a string. Here is a complete program to reverse a string using LINQ in C#.
using System;
using System.Linq;
class Program
{
static void Main()
{
string original = "Reverse";
string reversed = new string(original.Reverse().ToArray());
Console.WriteLine(reversed); // Output: "esreveR"
}
}
Reverse a C# string using Array.Reverse() with Char Array
Now, I will show you another way to reverse a string using Array.Reverse() with Char Array in C#. Here is a complete program.
using System;
class Program
{
static void Main()
{
string original = "ArrayReverse";
string reversed = ReverseWithArrayReverse(original);
Console.WriteLine(reversed); // Output: "esreveRyarrA"
}
static string ReverseWithArrayReverse(string str)
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
The Array.Reverse() method where we manually convert the string to a char array and back in C#.
Conclusion
In this tutorial, I have explained how to write a C# program to reverse a string. I have shown you by executing a complete example. I have also explained a few other methods to reverse a string in C# like by using the StringBuilder, LINQ, and using Array.Reverse() with Char Array.
You may also like:
- How to Check If String Contains a Character in C#?
- C# String Array Contains Multiple Values
- How to Compare String Value with String Array in C#?
- How to Compare String with Multiple Values in C#?
- Write a C# Program to Print Fibonacci Series
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…