Write a C++ program that displays a Letter Pyramid from a user-provided std::string. Prompt the user to enter a std::string and then from that string display a Letter Pyramid as follows: It's much easier to understand the Letter Pyramid given examples.

Example: Write a C++ program that displays a Letter Pyramid from a user-provided std::string. Prompt the user to enter a std::string and then from that string display a Letter Pyramid as follows: It's much easier to understand the Letter Pyramid given examples.

#include <iostream>
#include <string>

using namespace std;

int main() {

  string user_input {}, pyra {""}, space (user_input.length() - 1, ' ');

  cout << "Please enter a sequence of characters... ";
  cin >> user_input;

  for (size_t i{0}; i < user_input.length(); ++i) {

    pyra = user_input.substr(0, i + 1);

    cout << space << pyra;

    if (pyra.length() == 1) {
      cout << space;
    } else {
      for (auto j {pyra.length()}; j > 0; --j) {
        cout << pyra.at(j);
      }
    }
    space.erase(0, 1);
    cout << endl;
  }
  return 0;
}

Tags:

Misc Example