Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

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

0.00/5 (No votes)
25 Jul 2011CPOL 6.8K  
Another way,public static class ReverseStringExtension{ public static string Reverse(this string dataToRevese) { Func reverseFunc = (dataToFunc) => { StringBuilder reverseBuilder = new StringBuilder(); for (int index =...
Another way,

C#
public static class ReverseStringExtension
{
    public static string Reverse(this string dataToRevese)
    {
        Func<string, string> reverseFunc = (dataToFunc) =>
        {
            StringBuilder reverseBuilder = new StringBuilder();
            for (int index = 0; index < dataToFunc.Length; )
                reverseBuilder.Append(dataToFunc[dataToFunc.Length - ++index]);
            return reverseBuilder.ToString();
        };

        return string.IsNullOrEmpty(dataToRevese) ? default(string) : reverseFunc(dataToRevese);
    }
}



Usage:
C#
string stringRequireReverse = "Hello world, I am going to be reversed.";
stringRequireReverse = stringRequireReverse.Reverse();

Output:
.desrever eb ot gniog ma I ,dlrow olleH

License

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