C# C# Console Methods

C# Program to Reverse a Sentence Using Recursion

In this example, you’ll learn to reverse a given sentence using a recursive loop in C#.

In the following program, we’ve a recursive function reverse().

On each iteration, we add (concatenate) the result of next reverse() function to the first character of sentence using sentence[0].

The recursive call must be before the sentence[0], because that way the last characters will start adding to the left hand side. If you reverse the order, you’ll end up with the original sentence.

In the end, we end up with an empty sentence and reverse() returns the reversed sentence.

C# Code:

When you run the program, the output will be:

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.