Display a string in reverse by using recursion : Method Recursion : Class C# Examples


C# Examples » Class » Method Recursion »

 

Display a string in reverse by using recursion









    
using  System;  
    
class  RevStr  {  
  
    public  void  displayRev(string  str)  {  
        if(str.Length  >  0)    
            displayRev(str.Substring(1,  str.Length-1));  
        else    
            return;  
  
        Console.Write(str[0]);  
    }  
}  
  
class  MainClass  {  
    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();  
    }  
}
    
   
  
   



Output

Original string: this is a test
Reversed string: tset a si siht


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Method Recursion