fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. string user_input {}, pyra {""};
  9.  
  10. cout << "Please enter a sequence of characters... ";
  11. cin >> user_input;
  12. cout<<endl;
  13. string space (user_input.length() - 1, ' '); // create it here !!
  14.  
  15. for (size_t i{0}; i < user_input.length(); ++i) {
  16. pyra = user_input.substr(0, i+1 );
  17.  
  18. cout << space << pyra;
  19.  
  20. for (auto j {pyra.length()}; j > 0; --j) {
  21. cout << pyra.at(j-1); // don't go out of bounds
  22. }
  23. space.erase(0, 1);
  24. cout << endl;
  25. }
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5508KB
stdin
BLAR
stdout
Please enter a sequence of characters... 
   BB
  BLLB
 BLAALB
BLARRALB