Recursive string reversal function in javascript?

前端 未结 12 1666
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 08:48

I\'m a pretty experienced frontend engineer with a weak CS background. I\'m trying to get my head around the concept of recursion. Most of the examples and purported explana

12条回答
  •  余生分开走
    2020-12-03 09:10

    This is a pretty straightforward C# implementation of the algorithm you asked for. I think it could be rewritten in javascript pretty easily.

    /*
    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(); 
      } 
    }
    

提交回复
热议问题