Search code examples
cword-wrap

Need to word-wrap text in C


Well i have simple homework. I receive text from teacher server and need to make it look like chat. Some text is on left side some on right side. That is what i have done and work.

i = 0;
ratac = 0;

int padding = 1;
while (recvbuf[i] != '\0') {
    if (ratac == 61) {
        padding = 1;
        ratac = 0;
    }

    if (padding) {
        printf("%+60c", recvbuf[i]);
        padding = 0;
    }
    else {
        printf("%c", recvbuf[i]);
    }
    ratac++;
    i++;

};

But i need to add word wrap and dont know how to do it. (one word cant be on two lines). Know it looke like this

                                       WORD WORD WORD WO
                                       RD WORD WORD WORD

And I need to make it looks like this

                                   WORD WORD WORD 
                                   WORD WORD WORD
                                   WORD

Thank for any help.


Solution

  • Of course you need first to check there is enough place before to write a word

    supposing

    • the separator are only spaces
    • all the separators are produced (even consecutively or at the beginning of a line)
    • in case a word is longer than the width it is not cut (case of azeryuiop in the execution later)

    a proposal from your code where the program receives in argument the left margin, the width and the string

    #include <stdio.h>
    
    int length(char * p)
    {
      char * p2 = p;
    
      while (*(++p2) && *p2 != ' ')
        ;
    
      return (int)(p2 - p);
    }
    
    int main(int argc, char ** argv)
    {
      if (argc != 4) {
        printf("Usage : %s <left margin> <width> <string>\n", *argv);
        return -1;
      }
    
      int margin, width;
    
      if ((sscanf(argv[1], "%d", &margin) != 1) || (margin < 0)) {
        fprintf(stderr, "invalid margin\n");
        return -1;
      }
      if ((sscanf(argv[2], "%d", &width) != 1) || (width < 1)) {
        fprintf(stderr, "invalid width\n");
        return -1;
      }
    
      char *  recvbuf = argv[3];
    
      int i = 0;
      int ratac = width;
    
      while (recvbuf[i] != '\0') {
        if (ratac >= width) {
          if (i) 
            putchar('\n');
          for (int j = 0; j != margin; ++j)
            putchar(' ');
          ratac = 0;
        }
    
        if (recvbuf[i] == ' ') {
          putchar(' ');
          i += 1;
          ratac += 1;
        }
        else {
          int len = length(recvbuf + i);
    
          if (((ratac + len) <= width) || (ratac == 0)) {
            ratac += len;
            while (len--)
              putchar(recvbuf[i++]);
          }
          else
            ratac = width;
        }
      }
      putchar('\n');
    }
    

    Compilation and execution :

    pi@raspberrypi:~ $ gcc -pedantic -Wall -Wextra p.c
    pi@raspberrypi:~ $ ./a.out 2 7 "a zer az e r az e aze azeryuiop aze" 
      a zer 
      az e r 
      az e 
      aze 
      azeryuiop
       aze
    pi@raspberrypi:~ $