fork download
  1. #include <iostream>
  2. #include<string>
  3.  
  4. int main() {
  5. const std::size_t rows = 30;
  6. const std::size_t cols = 70;
  7.  
  8. std::string pattern[rows];
  9.  
  10. // initialize strings in pattern to be of length "cols" and all spaces:
  11. for (std::size_t i = 0; i < rows; ++i)
  12. pattern[i] = std::string(cols, ' ');
  13.  
  14. // top/bottom:
  15. for (std::size_t i = 0; i < cols; ++i)
  16. pattern[0][i] = pattern[rows - 1][i] = 'x';
  17.  
  18. // left/right:
  19. for (std::size_t i = 1; i < rows - 1; ++i)
  20. pattern[i][0] = pattern[i][cols - 1] = 'x';
  21.  
  22. // print the pattern out, taking advantage of the fact that each row is a string and
  23. // cout knows how to print the whole row:
  24. for (std::size_t row = 0; row < rows; ++row)
  25. std::cout << pattern[row] << '\n';
  26.  
  27. std::cout << '\n';
  28. std::cin.ignore(100, '\n');
  29.  
  30. // We want to insert "Hello" into the box without distorting the boundary:
  31. {
  32. std::size_t insertion_row = 5;
  33. std::size_t insertion_col = 5;
  34. std::string insert_me = "Hello";
  35.  
  36. for (std::size_t i = 0; i < insert_me.length(); ++i)
  37. pattern[insertion_row][insertion_col + i] = insert_me[i];
  38. }
  39.  
  40. // See our handiwork
  41. for (std::size_t row = 0; row < rows; ++row)
  42. std::cout << pattern[row] << '\n';
  43.  
  44. std::cout << '\n';
  45. std::cin.ignore(100, '\n');
  46.  
  47. // An alternate way to insert text:
  48. {
  49. std::size_t insertion_row = 10;
  50. std::size_t insertion_col = 5;
  51. std::string insert_me = "Goodbye";
  52.  
  53. pattern[insertion_row].replace(insertion_col, insert_me.length(), insert_me);
  54. }
  55.  
  56. // See our handiwork
  57. for (std::size_t row = 0; row < rows; ++row)
  58. std::cout << pattern[row] << '\n';
  59.  
  60. std::cout << '\n';
  61. }
Success #stdin #stdout 0s 3468KB
stdin
n
n
n
stdout
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x    Hello                                                           x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x    Hello                                                           x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x    Goodbye                                                         x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
x                                                                    x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx