2

Class prints a string to the console. How to make the width of output lines is equal characterWidth = 40, ie after 40 characters was transferred to a new line?

#include <string>
#include <iostream>

class StringProcessing {
  public:
      StringProcessing() : characterWidth(40),
                     textToBeFormatted("NULL") {}

      inline void StringProcessing::initString() {
          textToBeFormatted =
              "text    some text   some text some text   some text some text"
              "text some text  some text    some text some text some text"
              "text    some      text some    text some   text some text"
              "text some  text some    text some    text some text some text"
              "text   some     text some text some   text some text some text";
      }

      inline void displayString()
        { std::cout << textToBeFormatted << std::endl; }
  private:    
      int characterWidth;
      std::string textToBeFormatted;
};

I have an idea but here the words in the console are cut off, so they need to be transferred to the next line and perform a width alignment

inline void displayString()
      {
         const std::string& s = textToBeFormatted;
         for (int i = 0; i < s.length() / 40 + (bool)(s.length() % 40); ++i)
         {
            std::cout << std::left
                      << std::setfill(' ')
                      << std::setw(40)
                      << s.substr(i * 40, 40)
                      << std::endl;
         }
      }
Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • And what's the problem? Are you trying to justify the text? It reminds me dynamic programming, this is a kind of "book example": http://stackoverflow.com/q/18200593/1168156 – LihO Jan 31 '14 at 18:10

1 Answers1

0

Here is the answer suitable for me

#include <string>
#include <iostream>
#include <iomanip>

class StringProcessing
{
public:
    StringProcessing() : characterWidth(40),
        textToBeFormatted("NULL") {}

    inline void initString() {
        textToBeFormatted = "text    some text   some text some text   some text some text"
            "text some text  some text    some text some text some text"
            "text    some      text some    text some   text some text some text"
            "text some  text some    text some    text some text some text"
            "text   some     text some text some   text some text some text";
    }

    inline void displayString()
    {
        const std::string& s = textToBeFormatted;
        const int& width = characterWidth;
        for (int current = 0; current < s.length();)
        {
            if (s.length() < width)
            {
                output(s);
                break;
            }
            if (s.length() - current < width)
            {
                output(s.substr(current));
                break;
            }
            std::string substr = s.substr(current, width);
            current += width;
            size_t space = substr.rfind(' ');
            if (space != std::string::npos && (substr[width - 1] != ' ' &&
                (s.length() > current && s[current] != ' ')))
            {
                current -= width - space - 1;
                substr = substr.substr(0, space + 1);
            }
            output(substr);
        }
    }
private:
    inline void output(const std::string& s)
    {
        std::cout << setfill(' ') << std::right << std::setw(characterWidth) << s << std::endl;
    }
    int characterWidth;
    std::string textToBeFormatted;
};
Amazing User
  • 3,473
  • 10
  • 36
  • 75