scala reverse word with recursion

    0

    0

    RABINDRA JAISWAL

    This code prints "hello world-reverse"

    object reverseS extends App{
      //println("hello world")
    
      def reverseString(word:String): String = {
        if (word.length ==1) return word
        else {
          //println("to reverse " +word+"-first i need to reverse-" + word.tail)
          var reversed = reverseString(word.tail) + word.head
          //println("computed reverse of-"+ word)
          return reversed
    
        }
      }
    
      println(reverseString("abcde"))
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.