Spaces to tabs converter I wrote a program to convert spaces to tabs. If there are 4 spaces it should convert them to tab. Please let me know how to improve it.

#include &lt;stdio.h&gt;
#define TABVALUE 4
int c, d, s;

int main(void) {
    c = s = d = 0;
    while ((c = getchar()) != EOF) {
        if (c == ' ') {
            s++;
            for (int j = 0; j &lt; TABVALUE - 1; j++) {
                d = getchar();
                if (d == ' ') {
                    s++;
                }
            }
            if (s == TABVALUE) {
                putchar('\t');
                s = 0;
            }
        } else {
            putchar(c);
        }
    }
    return 0;
}
Bugs
j unconditionally loops to TABVALUE - 1. This is wrong:

If d happens to not be a space, it is not printed out.
If next d happens to be a space, it is still counted.

Test against a  b  cdef. The output is a\tf.