How do you reverse a string in C#

How do you reverse a string in C#

We can reverse a string in C# using loop and arithmetic operators. In this program, we are getting string as input from the user and reversing that string.

Let’s see a simple C# example to reverse a given string.

class CodeLerner
{
static void Main(string[] args)
{
string str, revstr = “”;
Console.Write(“enter Str..“);
str=Console.ReadLine();

for (int i = str.Length – 1; i >= 0; i–)
{
revstr = revstr + str[i];
}
Console.WriteLine(“reverse str…{0}“, revstr);
Console.ReadLine();
}
}

OutPut :

Input :  Enter String As following ABCDEFGH

OutPut :  HGFEDCBA

How do you reverse a string in C#

Learn More Tutorial :

5 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *