Solved

Algorithm for Word Wrap

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now