How to print text diagonally from left to right recursively in Swift

1 Answer

0 votes
import Foundation

func printTextDiagonallyRecursivelyLTR(_ spaces: String, _ text: String) {
    if !text.isEmpty {
        print(spaces + String(text.first!))
        let rest = String(text.dropFirst())
        printTextDiagonallyRecursivelyLTR(spaces + " ", rest)
    }
}

printTextDiagonallyRecursivelyLTR("", "HELLO WORLD")



/*
run:

H
 E
  L
   L
    O
      
      W
       O
        R
         L
          D

*/

 



answered Apr 8 by avibootz

Related questions

...