Answers for "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."

C++
0

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;
}
Posted by: Guest on November-11-2020

Code answers related to "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."

Browse Popular Code Answers by Language