Click here to Skip to main content
Click here to Skip to main content

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

By , 26 Jul 2011
 

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

License

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

About the Author

hemantwithu
Software Developer
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalwhats the point of this ?memberXmen W.K.27 Jul '11 - 0:10 
whats the point of this ?
GeneralReason for my vote of 1 What is the utility of this?memberJaime 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...mvpPrerak Patel22 Jul '11 - 23:45 
Reason for my vote of 1
Globally known age old problem since C era.
General[edit] removed email address.subeditorIndivara22 Jul '11 - 12:14 
[edit] removed email address.
GeneralFew notesmemberMika Wendelius22 Jul '11 - 0:55 
I guess the obvious question you haven't answered yet is: What's the benefit in this solution compared to using for example Array.Reverse method.[^]. Consider adding this to your post.
 
Another thing is that you shouldn't include your e-mail address into the post. If someone sends a message to this post, you will be automatically notified via e-mail. Revealing the actual e-mail address only increases the amount of spam you'll get.

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by hemantwithu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid