Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

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

By , 23 Jul 2011
Rate this:
Please Sign up or sign in to vote.

Yet some another way to reverse strings is given below. Take a look:

private static string Reverse(string str)
   if (str.Length == 1)
       return str;
       return str[str.Length - 1] + Reverse(str.Substring(0, str.Length - 1));


public static string Reverse(string x)
    char[] charArray = new char[x.Length];
    int len = x.Length - 1;
    for (int i = 0; i <= len; i++)
       charArray[i] = x[len - i];
    return new string(charArray);

and using bitwise XOR:

public static string ReverseXor(string s)
    char[] charArray = s.ToCharArray();
    int len = s.Length - 1;
    for (int i = 0; i < len; i++, len--)
        charArray[i] ^= charArray[len];
        charArray[len] ^= charArray[i];
        charArray[i] ^= charArray[len];
    return new string(charArray);

In comparison of all of these three code snippet second way is faster that XOR and recursive implementation. So I will go for it. I will suggest you to recommend an old but excellent article focusing the performance of reversing algorithm.
take a look there-[Performance: Fastest string reversing algorithms][^].

Apart it I will always go for Array.Reverse for reverse process because its simplest and fastest in all of them.


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

About the Author

Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
He keep himself on the desk of his imagination, hanging around with some inceptions.
Follow on   Twitter   Google+

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 23 Jul 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid