Skip to content

Bug of Exercise 1-22 #85

@ChenZhongPu

Description

@ChenZhongPu

fold_line.c is buggy because it fails to handle very long lines. The correct version:

#include <stdio.h>

#define MAX_COLUMN 10 // Define the maximum column width for a line

int main() {
  int c;
  char line_buffer[MAX_COLUMN + 2]; // Buffer to hold the current line being
                                    // built (+2 for safety)
  int i = 0;                        // Current index in the line_buffer
  int last_blank = -1; // Index of the last blank character (space or tab) found

  while ((c = getchar()) != EOF) {
    // Add the character to our line buffer and update its position.
    line_buffer[i] = c;

    // If the character is a blank, record its position.
    if (c == ' ' || c == '	') {
      last_blank = i;
    }

    // Handle explicit newlines from the input.
    if (c == '\n') {
      line_buffer[i + 1] = '\0'; // Null-terminate the string
      printf("%s", line_buffer); // Print the buffer as is
      i = 0;                     // Reset for the next line
      last_blank = -1;
      continue; // Skip to the next loop iteration
    }

    // Check if the buffer is full.
    if (i >= MAX_COLUMN) {
      // If the buffer is full, it's time to fold the line.

      if (last_blank != -1) {
        // --- CASE 1: We found a blank space to break at. ---

        // Print the line up to (but not including) the last blank.
        for (int j = 0; j < last_blank; j++) {
          putchar(line_buffer[j]);
        }
        putchar('\n'); // Fold the line here.

        // Now, move the rest of the line (the start of the next word)
        // to the beginning of the buffer for the next iteration.
        int new_i = 0;
        // Start from the character *after* the last blank.
        for (int j = last_blank + 1; j <= i; j++) {
          line_buffer[new_i] = line_buffer[j];
          new_i++;
        }
        i = new_i;
        last_blank = -1; // Reset since we are on a new line.
      } else {
        // --- CASE 2: No blank space found (a single very long word). ---

        // The character that triggered the break must be preserved.
        char trigger_char = line_buffer[i];

        // Temporarily terminate the string *before* the trigger character and
        // print.
        line_buffer[i] = '\0';
        printf("%s\n", line_buffer);

        // Start the new line with the trigger character.
        line_buffer[0] = trigger_char;
        i = 1; // The buffer now contains one character.
        last_blank = -1;
        continue;
      }
    }

    i++; // Move to the next position in the buffer.
  }

  // After the loop, print any remaining characters in the buffer.
  if (i > 0) {
    line_buffer[i] = '\0';
    printf("%s", line_buffer);
  }

  return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions