How to reverse a string in C#
In this tutorial, we are going to learn about how to reverse a string in C#.
Consider, we have the following string.
string s = "play";
Now, we need to reverse it like this yalp
.
To reverse a string in C#, first we need to convert the string into character array using ToCharArray()
method then pass it to the Array.Reverse()
method and convert back to string .
Here is an example:
using System;
class ReverseArray {
static void Main() {
string s = "play";
char[] arr = s.ToCharArray();
Array.Reverse( arr );
string result = new string( arr );
Console.WriteLine(result);
}
}
Output:
yalp