fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int MAX_LENGTH = 100;
  6.  
  7. char currentLine[MAX_LENGTH + 1][MAX_LENGTH + 1];
  8. int wordsLength[MAX_LENGTH] = {0};
  9.  
  10. int charCounter(int counter) {
  11. int chars = 0;
  12. for (int i = 1; i <= counter; ++i) {
  13. chars += strlen(currentLine[i]);
  14. }
  15. return chars;
  16. }
  17.  
  18. void formatLine(int wordsPerLine, int columns) {
  19. int spaces = wordsPerLine - 1, neededStars = columns - charCounter(wordsPerLine);
  20. for (int i = 1; spaces != 0; ++i) {
  21. int stars = neededStars / spaces;
  22. if (neededStars % spaces) {
  23. ++stars;
  24. }
  25. char addStars[MAX_LENGTH + 1];
  26. for (int j = 0; j < stars; ++j) {
  27. addStars[j] = '*';
  28. addStars[j + 1] = '\0';
  29. }
  30. --spaces;
  31. neededStars -= stars;
  32. cout << currentLine[i] << addStars;
  33. }
  34. cout << currentLine[wordsPerLine] << "\n";
  35. }
  36.  
  37. int main() {
  38. int noWords, lines, columns, wordsPerLine = 0, noLine = 0, charLine = 0;
  39. cin >> noWords >> lines >> columns;
  40. for (int i = 1; i <= noWords; ++i) {
  41. char word[MAX_LENGTH + 1];
  42. cin >> word;
  43. int wordLength = strlen(word);
  44. charLine += wordLength + 1;
  45. if (columns + 1 >= charLine) {
  46. strcpy(currentLine[++wordsPerLine], word);
  47. } else {
  48. ++noLine;
  49. formatLine(wordsPerLine, columns);
  50. if (noLine % lines == 0) {
  51. cout << "\n";
  52. }
  53. charLine = wordLength + 1;
  54. strcpy(currentLine[1], word);
  55. wordsPerLine = 1;
  56. }
  57. }
  58. formatLine(wordsPerLine, columns);
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5280KB
stdin
5 5 20
a
b
c
d
e
stdout
a****b****c****d***e