How to print text diagonally from left to right in Java

1 Answer

0 votes
public class DiagonalText {

    public static void printDiagonalText(String text) {
        for (int i = 0; i < text.length(); i++) {
            // print i spaces before the character
            for (int s = 0; s < i; s++) {
                System.out.print(" ");
            }
            System.out.println(text.charAt(i));
        }
    }

    public static void main(String[] args) {
        String text = "HELLO WORLD";
        printDiagonalText(text);
    }
}


/* 
run:

H
 E
  L
   L
    O
      
      W
       O
        R
         L
          D

*/

 



answered Apr 7 by avibootz
...