Solved

Algorithm for Word Wrap

Posted on 1997-11-19
1
1,242 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 100 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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