Display a string in reverse by using recursion
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Display a string in reverse by using recursion.
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";
RevStr rsOb = new RevStr();
Console.WriteLine("Original string: " + s);
Console.Write("Reversed string: ");
rsOb.displayRev(s);
Console.WriteLine();
}
}
Related examples in the same category