From 19fa3daff1447c633029e1730bf0355b21ee029f Mon Sep 17 00:00:00 2001 From: zlg Date: Mon, 26 Nov 2012 20:45:37 -0600 Subject: Solve Exercise 1-21: entab --- 1-20_detab.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to '1-20_detab.c') diff --git a/1-20_detab.c b/1-20_detab.c index 3101610..e1549e6 100644 --- a/1-20_detab.c +++ b/1-20_detab.c @@ -5,11 +5,15 @@ * "Write a program `detab` that replaces tabs in the input with the proper * number of blanks to space to the next tabstop. Assume a fixed set of * tabstops, say every 'n' columns. Should 'n' be a variable or a symbolic - * parameter? + * parameter?" * * Answer: 'n' should be a symbolic parameter. It's more apparent what's being * worked with and it's not susceptible to scope. Though, in this simple * program it really doesn't matter. + * + * To be correct, this file should use the isprint() function to ensure that + * 'column' only gets incremented when a printable character is being read. I'll + * come back to this when I learn more of the stdlib. */ #define TABWIDTH 8 @@ -30,12 +34,14 @@ int main(void) { ++column; } } else { - // non-tabs simply get output. - putchar(c); - ++column; - // Line-endings should reset the column counter after being output. if (c == '\n') { + // Line-endings should reset the column counter after being output. + putchar(c); column = 0; + } else { + // Now we can just output and increase column! + putchar(c); + ++column; } } } -- cgit v1.2.3-54-g00ecf