summaryrefslogtreecommitdiff
path: root/ch01/ex20-detab.c
blob: 78aae5a6d73d67fbad7b7a08bbfe1e817073bdbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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(' ');
}