aboutsummaryrefslogtreecommitdiff
path: root/1-20_detab.c
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2012-11-26 20:45:37 -0600
committerzlg <zlg@zlg.space>2012-11-26 20:45:37 -0600
commit19fa3daff1447c633029e1730bf0355b21ee029f (patch)
tree8a114d9edcc8ae343be161bb8dc4a8a8056ec332 /1-20_detab.c
parentSolve exercise 1.20: Detab (diff)
downloadknr-19fa3daff1447c633029e1730bf0355b21ee029f.tar.gz
knr-19fa3daff1447c633029e1730bf0355b21ee029f.tar.bz2
knr-19fa3daff1447c633029e1730bf0355b21ee029f.tar.xz
knr-19fa3daff1447c633029e1730bf0355b21ee029f.zip
Solve Exercise 1-21: entab
Diffstat (limited to '1-20_detab.c')
-rw-r--r--1-20_detab.c16
1 files changed, 11 insertions, 5 deletions
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;
}
}
}