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
Solved

Algorithm for Word Wrap

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 131
Unresolved External Symbols 3 101
Unable to start eclipse ? 17 152
Gaming Software 1 18
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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.

792 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