65.9K
CodeProject is changing. Read more.
Home

Reverse of a string without using the Reverse function in C# and VB

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.38/5 (12 votes)

Jul 21, 2011

CPOL
viewsIcon

96608

How to reverse a string without using the Reverse function in C# and VB.

This is a simple code snippet for reversing a string without using the Reverse function. For example, we have a string "KUMAR"; without using a function, we can reverse it using the code snippet below. Code is given in both C# and VBalso. This will be helpful to beginners. If you have any queries, please feel free to ask me.

class ReverseString
{
    public static void Main(string[] args)
    {
        string Name = "He is palying in a ground.";
        char[] characters = Name.ToCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = Name.Length - 1; i >= 0; --i)
        {
            sb.Append(characters[i]);
        }
        Console.Write(sb.ToString());
        Console.Read();
    }
}
VB:
Class ReverseString
    Public Shared Sub Main(args As String())
        Dim Name As String = "He is palying in a ground."
        Dim characters As Char() = Name.ToCharArray()
        Dim sb As New StringBuilder()
        For i As Integer = Name.Length - 1 To 0 Step -1
            sb.Append(characters(i))
        Next
        Console.Write(sb.ToString())
        Console.Read()
    End Sub
End Class