1
Reply

How to reverse string in C#

Kirtesh Shah

Kirtesh Shah

Dec 02
244
0
Reply

How to reverse string in C#

    Method 1: Using ToCharArray() and Array.Reverse()

    string str = “hello”;
    char[] charArray = str.ToCharArray();
    Array.Reverse(charArray);
    string reversed = new string(charArray);
    Console.WriteLine(reversed); // Output: “olleh”

    Method 2: Using LINQ
    using System;
    using System.Linq;

    string str = “world”;
    string reversed = new string(str.Reverse().ToArray());
    Console.WriteLine(reversed); // Output: “dlrow”