word wrap

This is a discussion on word wrap within the C Programming forums, part of the General Programming Boards category; How can I make a string full of text print without having to break a word and go to the ...

  1. #1
    max
    Guest

    word wrap

    How can I make a string full of text print without having to break a word and go to the other line??

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    ?

    where ? in console ?

  3. #3
    Unregistered
    Guest
    using printf.....I wanted top be printed in the screen

    ex: char line[]="hkjhk kjhkjhkjkjjkjkjj kjkjkjk jkjkljljkl ljljljl jljljlj jljljlj ljljl jljljljl jljljl jljll;;lk;lk; l;; k;k;k;k k;k;k; ;k;k;k;k";

    printf("%s",line);

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here...

    if you're looking for wordwrap in console,
    look here.....
    Code:
    void mywordwrap(FILE *fp, char string[], short maxLineWidth) {
    
       short startIndex;
       short endIndex;
       short actualLineWidth;
    
       if (fp == NULL || string == NULL || maxLineWidth <= 0) {
          return;
       }
    
       
       startIndex = 0;
       endIndex = strlen(string);
    
    
        while (startIndex < endIndex) {
    
          
         while (startIndex < endIndex && isspace( string[startIndex] )) {
    	 ++startIndex;
          }
    
          
          actualLineWidth = maxLineWidth;
          if (endIndex - startIndex > maxLineWidth) {
             while (actualLineWidth > 0 &&
                !isspace( string[ startIndex+actualLineWidth-1 ]) &&
                !isspace( string[ startIndex+actualLineWidth   ]) ) {
                --actualLineWidth;
             }
    	}
    
    
           if (startIndex < endIndex && actualLineWidth > 0) {
    	 fprintf(fp,"%.*s\n",actualLineWidth, &string[ startIndex ]);
          }
    
          
          startIndex += actualLineWidth;
       }
    }
    
    //syntax mywordwrap(stdout, chararray, 70);
    GOOD LUCK
    Last edited by moonwalker; 08-23-2002 at 11:25 AM.

  5. #5
    Unregistered
    Guest
    WOW can it be a bit simpler!!!

  6. #6
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    >WOW can it be a bit simpler!!!

    yeah, do printf a dozen times and then
    see where it's cutting off... go there and put a \n
    in the string.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How can I make a string full of text print without having to break a word and go to the other line??
    The problem with this is that you do not know the width of the console window, so it is difficult to know when to wrap the word to the next line. The simplest method is to print words until the next word will hit the edge of the console screen, insert a newline before the word and keep going. Once again this involves knowing how many characters one line of the console can hold before wrapping.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to a file with word wrap?
    By Djanvk in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2008, 05:09 AM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Word Wrap
    By sethjackson in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 04:35 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21