I don't know how i don't want a direct answer I want to know how can i. Many thanks in advance.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a word or sentence to be reversed: ");
string str = Console.ReadLine();
Console.WriteLine("**********");
Console.WriteLine("Your input is: ");
Console.WriteLine(str);
Console.ReadLine();//to give a little bit space between the outputs
Console.WriteLine("************");
Console.WriteLine("And it will be reversed as : ");
//call the Recursive Function in the class Recursive
str = Recursive.Recursive_Func(str);
Console.WriteLine(str);
Console.ReadLine();
}
}
We use Substring to print from the n-1 index in the string
and end it with the first index in the string which is [0].
class Recursive
{
public static string Recursive_Func(string str)
{
if (str.Length <= 1) //the program base case
{
return str;
}
else
{
return Recursive_Func(str.Substring(1)) + str[0];
}
}
}