CSM217: C for Computer Scientists

Lecture 2: Exercise 3: Solution

#include <stdio.h>

main() {
 char c; // store the character just read in a char, more efficient than int

 while ((c = getchar()) != EOF) {
  if (c == '\t') {
   printf("\\t");
  } else if (c == '\\') {
   printf("\\\\");
  } else if (c == '\b') {
   printf("\\b");
  } else
   putchar(c);
  }
 }
}