Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C# 4.0
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
2.38/5 (12 votes)
26 Jul 2011CPOL 94.9K   2   7
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.


C#
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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Sergey Alexandrovich Kryukov6-Jul-14 20:56
mvaSergey Alexandrovich Kryukov6-Jul-14 20:56 
GeneralMy vote of 1 Pin
Member 1058994326-Jun-14 21:48
Member 1058994326-Jun-14 21:48 
Generalwhats the point of this ? Pin
Xmen Real 27-Jul-11 0:10
professional Xmen Real 27-Jul-11 0:10 
GeneralReason for my vote of 1 What is the utility of this? Pin
Jaime Olivares25-Jul-11 16:35
Jaime Olivares25-Jul-11 16:35 
Reason for my vote of 1
What is the utility of this?
GeneralReason for my vote of 1 Globally known age old problem since... Pin
Prerak Patel22-Jul-11 23:45
professionalPrerak Patel22-Jul-11 23:45 
General[edit] removed email address. Pin
Indivara22-Jul-11 12:14
professionalIndivara22-Jul-11 12:14 
GeneralFew notes Pin
Wendelius22-Jul-11 0:55
mentorWendelius22-Jul-11 0:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.