﻿ C# Sharp Exercises: Get the reverse of a string - w3resource

# C# Sharp Exercises: Get the reverse of a string

## C# Sharp Recursion: Exercise-14 with Solution

Write a program in C# Sharp to get the reverse of a string using recursion.

Sample Solution:-

C# Sharp Code:

``````using System;
class RecExercise14
{
static void Main()
{
string str;
Console.WriteLine("\n\n Recursion : Get the reverse of a string :");
Console.WriteLine("----------------------------------------------");
Console.Write(" Input the string : ");
str = StringReverse(str);
Console.Write(" The reverse of the string is : ");
Console.Write(str);
Console.Write("\n");

}

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

Sample Output:

``` Recursion : Get the reverse of a string :
----------------------------------------------
Input the string : W3resource
The reverse of the string is : ecruoser3W
```

C# Sharp Code Editor:

```using System;
class RecExercise14
{
static void Main()
{
string str;
Console.WriteLine("\n\n Recursion : Get the reverse of a string :");
Console.WriteLine("----------------------------------------------");
Console.Write(" Input the string : ");
str = StringReverse(str);
Console.Write(" The reverse of the string is : ");
Console.Write(str);
Console.Write("\n");

}

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

Improve this sample solution and post your code through Disqus