Reverse String

Below is the code in two approach:


class ReverseString
    {
        public string Reverse(string str)
        {
            char[] chr = str.ToArray();
            StringBuilder news = new StringBuilder();

            for (int i = str.Length - 1; i >= 0; i--)
                news.Append(str[i]);

            return news.ToString();
        }

        public string ReverseRecursive(string str)
        {
            if (str.Length > 0)
                return str[str.Length - 1] + ReverseRecursive(str.Substring(0, str.Length - 1));
            else
                return str;
        }
    }

class MainReverse
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter the string: ");
            string s = Console.ReadLine();
            ReverseString rs = new ReverseString();
            Console.WriteLine("\nReverse: " + rs.Reverse(s));
            Console.ReadLine();
        }
    }

Refrences :
http://en.support.wordpress.com/code/posting-source-code/

Advertisements

Leave a comment

Filed under C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s