Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define TABINC 8
- int main()
- {
- int c, nb, nt, pos; //nb-num blanks; nt-num tabs; pos-pos in line
- nb = nt = 0;
- for(pos=1; (c=getchar())!=EOF; ++pos) // input a character
- if(c==' ') { // if its a space b4 tabstop
- if(pos%TABINC!=0) // count it
- ++nb; // reset nb at tabstop
- else {
- nb=0; // when tabstop reached with ' ' add a tab
- ++nt; // count the number of tabs required
- } // there may be more than 1 to add
- }
- else {
- for( ; nt>0; --nt) // note first arg missing
- putchar('\t');
- if(c=='\t') // if a tab has just been used reset nb
- nb=0;
- else
- for( ; nb > 0; --nb) // if blanks stopped incrementing b4
- putchar(' '); // tabstop print ' ' not '\t'
- putchar(c); // print all chars
- if(c=='\n') // newline reset pos to 0
- pos=0;
- else if(c=='\t')
- pos = pos+(TABINC-(pos-1)%TABINC)-1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment