How to print text diagonally from left to right in C++

1 Answer

0 votes
#include <iostream>
#include <string>

void printDiagonalText(const std::string& text) {
    for (size_t i = 0; i < text.size(); ++i) {
        std::cout << std::string(i, ' ') << text[i] << "\n";
    }
}

int main() {
    std::string text = "Hello World";
    
    printDiagonalText(text);
}


/*
run:
 
H
 e
  l
   l
    o
      
      W
       o
        r
         l
          d
 
*/

 



answered Apr 7 by avibootz
...