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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var