How can a given string be reversed using recursion?




  How can a given string be reversed using recursion?

Solution:

public static void RevesrseAWord(string str)
        {
            if ((str == null) || (str.Length <= 1))
                Console.Write(str);

            else
            {
                Console.Write(str[str.Length - 1]);
                RevesrseAWord(str.Substring(0, (str.Length - 1)));
            }

           // return newString;
        }

  static void Main(string[] args)
        {
            Console.WriteLine("=============================================================================");
            Console.WriteLine("8. How can a given string be reversed using recursion?");
            Console.WriteLine("=============================================================================");
            Program objprog = new Program();
            Program.RevesrseAWord("ShivKumar");
            Console.ReadLine();

        }


OutPut:

============================================================================
How do you reverse words in a given sentence without using any library method?
=============================================================================
ramuKvihS


Share:

No comments:

Post a Comment

Popular

Tags

Mobile

Recent Posts