How to Reverse a String in C#

In today’s tutorial, I am going to show you how to reverse a string. While I know this does not sound impressive, it does give you a look into how you can manipulate strings in the future. By the end of this tutorial, you will be able to split a string up into individual characters and then manipulate them.

Requirements

Difficulty Easy

We want to create an application that gets a string from the user and then reverses it. The string will then be displayed back to the user.

Let’s Get Started

To get started, we need to create an empty C# console application in Visual Studio. Check out my how to guide on creating a C# console application in Visual Studio to learn how to do this.

Now that we have our black console application we are ready to get started. The first thing we need to do is get the string we want to reverse from the user. To do this, inside the Main method of our application we ask the user to enter a string and then get that string and assign it to a string named “originalString.” See below for how to do this.

Console.Write("Please enter a string to reverse: ");
string originalString = Console.ReadLine();

After we get the string from the user, we want to do a little bit of defensive programming and check and see that the user did enter a string. We do this by checking to see that the value of originalString is null or empty. If the string is null or empty, then write to the console “You did not enter a string” otherwise reverse the string.

if (String.IsNullOrEmpty(originalString))
{
   Console.WriteLine("You did not enter a string.");
}
else
{
}

Now that we have gotten the string to reverse and

know that it is a valid string its time to reverse it. To do this, we are going to break the string down into an array of characters. Then we will reverse the array. Next, we will create a new string out of the array of characters that are reversed. Once we have the reversed string will write it to the console thus meeting the requirement of displaying it back to the user. Sound complicated? It’s really not, check out how to do this below.

char[] originalStringArray = originalString.ToCharArray()
Array.Reverse(originalStringArray);
string reversedString = new string(originalStringArray);

Console.WriteLine($"The string reversed is: {reversedString}");

Insert the above code inside the else block so that it will only be run when the string is not null or empty.

See it in Action

Now it is time to run this application and try it out. Your string reversing application should look and function just like the application in the image below.

reverse-a-string-application

Congratulations! You now have an application that can reverse any string you want. You also know how to split a string into a character array so that you can manipulate the characters in this array. I hope you found this short tutorial helpful. If you did, please share this with a friend that you think would find it useful too.

[thrive_leads id=’1074′]