Solved

Algorithm for Word Wrap

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 137
SetCurrentDirectory path limit 7 104
Path to  STL Map header file 1 41
Issues with C++ Class 19 80
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

919 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

21 Experts available now in Live!

Get 1:1 Help Now