[SOLVED] Cut string by words

Issue

I need to cut string by words and put them in rows if row width can accept them. If not, they should be put in the next row.

OUTPUT:

--------------------
 The applicant must 
 have some hardware 
 construction 
 experience and be 
 fluent in C and 
 assembly language 
 programming.

Code:

#include <stdio.h>
void cut_words(const char *sentence, int width) {
  int i, j = 0,k, count = 0,rows=0,stop=0,rest;
   for (i = 0; i < width; i++)
    printf("-");
  printf("\n ");
  while (sentence[j] != '\0') {
    count++;
    printf("%c", sentence[j]);
    j++;
    if (count> width) {
      printf("\n ");
      count = 0;
    }
  }
}
int main() {
  const char sentence[1000] = "The applicant must have some hardware construction experience and be fluent in C and assembly language programming.";
  int width = 20;
  cut_words(sentence, width);
  return 0;
}
  • Note: auxiliary strings cannot be used.

Solution

I’m going to guess you’re supposed to do this without using library functions. Line filling and breaking text is a classic exercise. I don’t know if you’re expected to get a word at a time to fill the lines, such that multiple spaces in the input get collapsed. If not, another approach is to start at a nonspace, look at where the width would fill, then back up if you’re at a space or in the middle of a word. Print what you then have, and start again with the next word.

void cut_words(const char *sentence, int width) {
    char *s = (char *)sentence;
    for (int i = 0; i < width; i++)
        printf("-");
    printf("\n");
    // skip leading spaces
    while (*s && *s == ' ')
        s++;
    while (strlen(s) > width) {
        // *s must be nonspace and not EOS
        char *e = s + width - 1;
        // *e is last char that can possibly fit
        // *e cannot be EOS nor can e[1]
        // back up to nonspace if at space
        while (*e == ' ')
            e--;
        // *e is inside or end of word
        if (e[1] != ' ') {
            // not end of word; must be inside
            // back up to prev space
            while (e > s && *e != ' ')
                e--;
            // if e == s then no word break
            // must assume word is longer than width
            if (e == s)
                e = s + width - 1;
            else
                // find end of prev word
                while (e > s && *e == ' ')
                    e--;
        }
        // *e is end of word
        int k = e - s + 1;
        printf("%*.*s\n", k, k, s);
        s += k;
        while (*s && *s == ' ')
            s++;
    }
    if (*s)
        printf("%s\n", s);
}

Now, see if you can do that using just indexes instead of pointers. Or, more challenging, try to step along the string and find the beginning and end of each word and print them, keeping track of how much space is left in each row.

Answered By – raygard

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *