Last active
August 29, 2015 14:04
-
-
Save Learath2/a71e79cb5832d5a7fe4d to your computer and use it in GitHub Desktop.
K&R2 Exercise 1-21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*K&R2 Exercise 1-21 "entab"*/ | |
#include <stdio.h> | |
#define TABSTOP 8 | |
int mod(int, int); | |
int main() | |
{ | |
int c, sc, col; | |
sc = 0; | |
for(col = 1; (c = getchar()) != EOF; ++col){ | |
if(c == ' '){ | |
++sc; | |
if(mod(col, TABSTOP) == 0){ | |
putchar('\t'); | |
sc = 0; | |
} | |
} | |
else{ | |
if(sc > 0) | |
for(; sc > 0; --sc) | |
putchar(' '); | |
if(c == '\n') | |
col = 0; | |
putchar(c); | |
} | |
} | |
} | |
int mod(int dividend, int divisor) | |
{ | |
return dividend - (dividend/divisor * divisor); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment