From 3cbdab1805a49e3529c3b90932c11208578dd2cc Mon Sep 17 00:00:00 2001 From: Zhineng Li Date: Fri, 29 May 2026 09:14:45 +0800 Subject: ch01-20: replaces tabs with proper spaces --- ch01/ex20-detab.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ch01/ex20-detab.c (limited to 'ch01') diff --git a/ch01/ex20-detab.c b/ch01/ex20-detab.c new file mode 100644 index 0000000..78aae5a --- /dev/null +++ b/ch01/ex20-detab.c @@ -0,0 +1,40 @@ +#include + +#define TABSTOP 8 /* number of columns between tab stops */ + +void putspace(int len); + + +/* replace tabs with proper spaces */ +int main() +{ + int c; + int col; + int nspace; + + col = 0; + while ((c = getchar()) != EOF) { + if (c == '\t') { + nspace = TABSTOP - (col % TABSTOP); + col = col + nspace; + putspace(nspace); + } else if (c == '\n') { + col = 0; + putchar(c); + } else { + ++col; + putchar(c); + } + } + + return 0; +} + +/* print len spaces */ +void putspace(int len) +{ + int i; + + for (i = 0; i < len; ++i) + putchar(' '); +} -- cgit v1.2.3