?
Solved

Algorithm for Word Wrap

Posted on 1997-11-19
1
Medium Priority
?
1,251 Views
Last Modified: 2008-03-03
Does anyone know an algorithm to calculate word wrap if you
are given a sentence and a maximum of a line?
i.e. A sentence is 45 characters long, and the max only
allows 40 characters. Therefore if the sentence is 45 chars long then it will wrap the last word around if the last word starts at the 39th character in the sentence.
The only things given are the String and the max_line_lenght. Please let me know if anyone can help.

Thanks in advance
0
Comment
Question by:yn0t
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
anichini earned 200 total points
ID: 1173650
// I assume for the example that we are printing out to
// the ostream. This example does not handle tabs
// I assume that spaces are the only delimiters
// I also don't handle the case where a word is longer than
// the line length (but these modifications are not impossible)
void wrapLine(ostream &os, const char *p, int length)
{
   int charsleft = length;
   int size;
   const char *pNextWord;

   while(*p)
   {
      pNextWord = strchr(p, ' ');
      if(pNextWord == NULL)
      { // at end of line
        pNextWord = strchr(p, '\0');
        assert(pNextWord != NULL); // must be true!
      }
      size = pNextWord - p;
      // here you should check if the word is longer than the line size
      // if so take appropriate action for your app
      if(pNextWord - p > charsleft)
      {
         os << endl; // print endline (wrap)
         charsleft = length;
      }
      else
      {
         os.write(p, size);
         charsleft -= size;
         p = pNextWord;
         if(*p)
         {
            if(charsleft)
            {
              os << *p; // write delimiter
            } // otherwise we're going to wrap, eat the space
            p++;
         }
     }
  }
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question