Search code examples
ckernighan-and-ritchie

What are trailing blanks and tabs? K&R Exercise 1.18


In ex 1.18 of Kernighan & Ritchie's The C Programming Language, the reader is asked to write a program to remove trailing blanks and tabs, and to delete entirely blank lines. What is meant by trailing blanks and tabs? Are consecutive tabs or spaces trailing blanks and tabs? Also an entirely blank line means just consecutive '\n' s or will "\n \n" be considered a blank line too?


Solution

  • Trailing whitespace usually refers to whitespace that follows the last non-whitespace character of a line.

    I think "\n \n" should be considered a blank line (that's the blank vs. empty distinction used by the Apache StringUtils: empty strings have length 0, blank strings contain only whitespace). You'll delete them either way, since you'd delete trailing whitespace first, leaving them empty.