fork(1) download
  1. #include <iostream>
  2. #include <array>
  3. #include <string>
  4. using namespace std;
  5.  
  6. const size_t MaxRows = 3;
  7. const size_t MaxColumns = 3;
  8. const size_t MaxCells = MaxRows * MaxColumns;
  9.  
  10. using gridRow = std::array<char, MaxColumns>;
  11. using grid = std::array<gridRow, MaxRows>;
  12.  
  13. grid CreateGrid(const std::string &keyword)
  14. {
  15. grid g;
  16. std::array<bool, 256> lookup{};
  17. size_t numFilled = 0;
  18.  
  19. for(size_t i = 0; (i < keyword.size()) && (numFilled < MaxCells); ++i)
  20. {
  21. if (!lookup[static_cast<unsigned char>(keyword[i])])
  22. {
  23. lookup[static_cast<unsigned char>(keyword[i])] = true;
  24. g[numFilled / 3][numFilled % 3] = keyword[i];
  25. ++numFilled;
  26. }
  27. }
  28.  
  29. for(char ch = 'A'; (ch <= 'Y') && (numFilled < MaxCells); ++ch)
  30. {
  31. if (!lookup[static_cast<unsigned char>(ch)])
  32. {
  33. g[numFilled / 3][numFilled % 3] = ch;
  34. ++numFilled;
  35. }
  36. }
  37.  
  38. return g;
  39. }
  40.  
  41. int main()
  42. {
  43. grid myArray = CreateGrid("HELO");
  44. for(int row = 0; row < MaxRows; ++row) {
  45. for(int col = 0; col < MaxColumns; ++col) {
  46. cout << myArray[row][col] << " ";
  47. }
  48. cout << endl;
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0s 4336KB
stdin
Standard input is empty
stdout
H E L 
O A B 
C D F