lolamontes69

K+R Exercise1_21

Sep 5th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define TABINC 8
  4.  
  5. int main()
  6. {
  7.     int c, nb, nt, pos;                     //nb-num blanks; nt-num tabs; pos-pos in line
  8.  
  9.     nb = nt = 0;
  10.     for(pos=1; (c=getchar())!=EOF; ++pos)   // input a character
  11.         if(c==' ') {                        // if its a space b4 tabstop
  12.             if(pos%TABINC!=0)               // count it
  13.                 ++nb;                       // reset nb at tabstop
  14.             else {
  15.                 nb=0;                       // when tabstop reached with ' ' add a tab
  16.                 ++nt;                       // count the number of tabs required
  17.             }                               // there may be more than 1 to add
  18.         }
  19.         else {
  20.             for( ; nt>0; --nt)              // note first arg missing
  21.                 putchar('\t');
  22.             if(c=='\t')                     // if a tab has just been used reset nb
  23.                 nb=0;
  24.             else
  25.                 for( ; nb > 0; --nb)        // if blanks stopped incrementing b4
  26.                     putchar(' ');           // tabstop print ' ' not '\t'
  27.                 putchar(c);                 // print all chars
  28.                 if(c=='\n')                 // newline reset pos to 0
  29.                     pos=0;
  30.                 else if(c=='\t')
  31.                     pos = pos+(TABINC-(pos-1)%TABINC)-1;
  32.         }
  33.                    
  34. }
Advertisement
Add Comment
Please, Sign In to add comment