home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / format1 / format1.cxx next >
C/C++ Source or Header  |  1994-10-14  |  1KB  |  46 lines

  1.  
  2.  
  3. // A very simple text-formatting program: simply fills out each line with
  4. // blanks so that the line extends to 75 chars.
  5. //
  6. // M. A. Sridhar
  7. // Feb 13, 1994
  8. // Redone with iostreams: 6/28/94
  9.  
  10. #include "base/base.h"
  11. #include <iostream.h>
  12. #include <iomanip.h>
  13. main ()
  14. {
  15.     short max_line_length = 72;
  16.     CL_String line;
  17.     while (line.ReadLine (cin)) {
  18.         CL_StringSequence words = line.Split ();
  19.         register short word_count = words.Size();
  20.         // Find the sum of the lengths of all the words
  21.         if (word_count > 1) {
  22.             short word_len_sum = 0;
  23.             for (short i = 0; i < word_count; i++)
  24.                 word_len_sum += words[i].Size();
  25.             short space = max_line_length - word_len_sum;
  26.             short space_per_word = space / (word_count - 1);
  27.             short leftover = space % (word_count - 1);
  28.             for (i = 0; i < word_count - 1; i++) {
  29.                 cout << words[i] << setw (space_per_word) << ' ';
  30.                 if (leftover) {
  31.                     cout << ' ';
  32.                     leftover--;
  33.                 }
  34.             }
  35.         }
  36.         if (word_count >= 1)
  37.             cout << words[word_count-1] << endl;
  38.         else // Empty line
  39.             cout << endl;
  40.     }
  41. }
  42.  
  43.  
  44.  
  45.         
  46.