Reverse a string by using recursion in CSharp
Description
The following code shows how to reverse a string by using recursion.
Example
/* w w w . ja va2 s . co m*/
using System;
class RevStr {
// Display a string backwards.
public void displayRev(string str) {
if(str.Length > 0)
displayRev(str.Substring(1, str.Length-1));
else
return;
Console.Write(str[0]);
}
}
public class RevStrDemo {
public static void Main() {
string s = "this is a test from java2s.com";
RevStr rsOb = new RevStr();
Console.WriteLine("Original string: " + s);
rsOb.displayRev(s);
}
}
The code above generates the following result.