summaryrefslogtreecommitdiff
path: root/ch01
diff options
context:
space:
mode:
authorZhineng Li <im@zhineng.li>2026-05-29 09:14:45 +0800
committerZhineng Li <im@zhineng.li>2026-05-29 09:14:45 +0800
commit3cbdab1805a49e3529c3b90932c11208578dd2cc (patch)
tree688e6f06694fce51a3733bd7670bc1c57e6650e7 /ch01
parent70aedfed18efdb01ef1727a0bbf048fac65c77c3 (diff)
downloadc-knr-exercises-3cbdab1805a49e3529c3b90932c11208578dd2cc.tar.gz
c-knr-exercises-3cbdab1805a49e3529c3b90932c11208578dd2cc.zip
ch01-20: replaces tabs with proper spaces
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(' ');
+}