summaryrefslogtreecommitdiff
path: root/ch01
diff options
context:
space:
mode:
Diffstat (limited to 'ch01')
-rw-r--r--ch01/ex20-detab.c40
1 files changed, 40 insertions, 0 deletions
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 <stdio.h>
+
+#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(' ');
+}