Skip to content

Bug of Exercise 1-18 #82

@ChenZhongPu

Description

@ChenZhongPu

trailing_blanks.c is buggy because it assumes that there is always a new line.

The correct version:

void remove_trailing_blank(char line[], int len) {
  int end = len - 1;

  int has_newline = (line[end] == '\n');
  if (has_newline) {
    end--;
  }

  while (end >= 0 && (line[end] == ' ' || line[end] == '\t')) {
    end--;
  }

  if (has_newline) {
    line[++end] = '\n';
  }

  line[end + 1] = '\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