Skip to content

Instantly share code, notes, and snippets.

@Learath2
Last active August 29, 2015 14:04
Show Gist options
  • Save Learath2/a71e79cb5832d5a7fe4d to your computer and use it in GitHub Desktop.
Save Learath2/a71e79cb5832d5a7fe4d to your computer and use it in GitHub Desktop.
K&R2 Exercise 1-21
/*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