fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm> // Added for std::count
  6.  
  7. // Function to split a string into words
  8. std::vector<std::string> splitString(const std::string &input) {
  9. std::vector<std::string> words;
  10. std::istringstream iss(input);
  11. std::string word;
  12. while (iss >> word) {
  13. words.push_back(word);
  14. }
  15. return words;
  16. }
  17.  
  18. // Function to justify text
  19. std::string justifyText(const std::string &text, int lineLength) {
  20. std::vector<std::string> words = splitString(text);
  21. std::string result = "";
  22. std::string currentLine = "";
  23.  
  24. for (const std::string &word : words) {
  25. if (currentLine.empty()) {
  26. currentLine = word;
  27. } else {
  28. if (currentLine.length() + word.length() + 1 <= lineLength) {
  29. currentLine += " " + word;
  30. } else {
  31. // Distribute spaces evenly in the line
  32. int spacesToAdd = lineLength - currentLine.length();
  33. if (spacesToAdd > 0) {
  34. int spaceCount = std::count(currentLine.begin(), currentLine.end(), ' '); // Corrected
  35. int spacesPerGap = spacesToAdd / spaceCount;
  36. int extraSpaces = spacesToAdd % spaceCount;
  37. size_t pos = currentLine.find(' ');
  38. while (pos != std::string::npos) {
  39. currentLine.insert(pos, spacesPerGap, ' ');
  40. if (extraSpaces > 0) {
  41. currentLine.insert(pos, 1, ' ');
  42. extraSpaces--;
  43. }
  44. pos = currentLine.find(' ', pos + spacesPerGap + 1);
  45. }
  46. }
  47.  
  48. result += currentLine + "\n";
  49. currentLine = word;
  50. }
  51. }
  52. }
  53.  
  54. if (!currentLine.empty()) {
  55. result += currentLine;
  56. }
  57.  
  58. return result;
  59. }
  60.  
  61. int main() {
  62. std::string text = "Ok so I have to write a program which reads a text and then formats it by adding spaces between the words. The output text must have exactly 60 characters in each line. The most spaces between words shouldn't differ by more than one space from the least spaces between words (of the same line). More spaces should be furthest to the right. If needed, the program can also move some words from one line to the next, in order to not exceed the 60 character limit.";
  63.  
  64. int lineLength = 60;
  65.  
  66. std::string justifiedText = justifyText(text, lineLength);
  67. std::cout << justifiedText << std::endl;
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0.01s 5292KB
stdin
dgdgdfgd fgdg dg
gdfgdg dgdg dgdg 
dgdg dgdg dgdg dgdg d
stdout
Ok  so I have to write a program which reads a text and then
formats    it by adding spaces between the words. The output
text  must have exactly 60 characters in each line. The most
spaces between words shouldn't differ by more than one space
from the least spaces between words (of the same line). More
spaces       should be furthest to the right. If needed, the
program  can also move some words from one line to the next,
in order to not exceed the 60 character limit.