SHARE
TWEET

Pyramid from Letters (C++)

insta_guru Jul 28th, 2018 23 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string getPyramid(const string&);
  7.  
  8. int main()
  9. {
  10.     string letters {""};
  11.     cout << "Give me some letters: ";
  12.     cin >> letters;
  13.     cout << getPyramid(letters);
  14. }
  15.  
  16. string getPyramid(const string &letters)
  17. {
  18.     string pyramid {""};
  19.    
  20.     for (int i {0}; i < (int)letters.length(); i++)
  21.     {
  22.         for (int j {0}; j < (int)letters.length() - 1 - i; j++) pyramid += ' '; //spaces
  23.         for (int j {0}; j <= i; j++) pyramid += letters.at(j);  //begin to center
  24.         for (int j {i-1}; j >= 0; j--) pyramid += letters.at(j); //center (exclusive) to begin
  25.         pyramid += '\n';    //endline
  26.     }
  27.    
  28.     return pyramid;
  29. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top