reverse a string in csharp

To reverse a string in C#, you can use the built-in Reverse() method of the System.Linq namespace. Here's how you can use it:

main.cs
using System.Linq;

string myString = "hello world";
char[] charArray = myString.ToCharArray();
Array.Reverse(charArray);

string reversedString = new string(charArray);

Console.WriteLine(reversedString); // Output: "dlrow olleh"
231 chars
10 lines

Alternatively, you can loop through the characters of the string and add them to a new string in reverse order:

main.cs
string myString = "hello world";
string reversedString = "";

for (int i = myString.Length - 1; i >= 0; i--)
{
    reversedString += myString[i];
}

Console.WriteLine(reversedString); // Output: "dlrow olleh"
209 chars
10 lines

gistlibby LogSnag