Thread: Wrapping words during printf from a string so words are never broken apart

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    21

    Wrapping words during printf from a string so words are never broken apart

    I'm trying to write a program that wraps words to the next line if there isn't room for them in the current column. I have a function I've written but it doesn't work (apparently strcmp() only works with consts ). This could is by no means perfect, in fact I'm sure it's terrible.

    Example output:
    Line read from file using fgets: "I like rice"

    If max column width is 4 lets say, it should print out:
    I
    like
    rice

    Here's my current function: (does not work in the slightest)
    Code:
    void printWordsWrap(const char *line, int maxLineLength)
    {
        int i = 0;
        int j = 0;
        char *space = " ";
        int counter = -1;
        int startPosition = 0;
    
    
        for (i = 0; i < maxLineLength; i++)
        {
            if (strcmp(space, line[i]) != 0) // ERROR: (line is underlined) says argument of type char is incompatble with const char* 
            {
                counter++; // increment counter if it's not a space
            }
            if (strcmp(space, line[i]) == 0) // check to see if current char is a space, also, same error here
            {
                if (counter < maxLineLength) // checks to see if the current counter increment will fit in the column
                {
                    for (j = startPosition; j < counter; j++) //prints the characters associated with how how the counter got
                    {
                        printf("%c", line[j]);
                    }
                    startPosition = counter; // sets startposition to where we left off, so we aren't starting at the start of the line everytime
                }
                else
                {
                    printf("\n"); // throw a new line in there.
                    for (j = startPosition; j < counter; j++) // this assumes no words are longer then the column width
                    {
                        printf("%c", line[j]); // print the word
                    }
                    startPosition = counter;
                }
            }
        }
        return;
    }

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    strcmp() - compares strings.

    In your function, you are comparing a string and a char, so the error.

    To compare two char, a char from char array and a space, do something like this:

    (line[i] != ' ')

    You will have to make additional changes to make the function work.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are always longer words that will be torn apart by narrow columns - try and print "breakfast" in a column 4 chars wide.

    Here's how I'd do it:

    Code:
    #define WIDTH 4
    
    get the len of the string to be printed
    nextBreak = 0; //# of chars until next word break or newline
    c=0;
    for(i=0;i<len;i++) {  iterate over each letter
       if(str[i]==' ' || str[i]=='\n') {  //need a new newBreak
           assign nextBreak to i+1
           //now scout ahead to find the next word break or newline
           while(str[nextBreak]!= ' ' && str[nextBreak] != '\n') {
                if(!str[nextBreak]) break;
                ++nextBreak;
             }
             now subtract i from nextBreak and assign it to nextBreak
             so you have current info on how far it is to the next break
         }
          if(c plus nextBreak greater than WIDTH) {
             print a newline   
             c=-1;             //reset c so it will be 0 at the top of the loop
             if(str[i]==' ')   //don't print a space at the far left column
                ++i;        //just iterate over it
          }
          
          print str[i]     //ready to print
          --nextBreak;    //closer to nextBreak
          ++c;       //increment the current column #
       }
    With this type of logic, if you try to print "breakfast" in a width of 4 you'll get:

    b
    r
    e
    a
    k
    fast

    So it's not up to newspaper printing, just yet. <smile>

    But any word that is less than or equal to the width, will print OK.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    21
    Thanks a ton!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words in a string
    By mrbains in forum C Programming
    Replies: 8
    Last Post: 10-25-2010, 08:47 AM
  2. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  3. number of words in a string
    By jackhasf in forum C Programming
    Replies: 11
    Last Post: 12-26-2009, 03:37 PM
  4. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM
  5. extracting words from an array of words
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 11:21 PM