Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.42 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ## Example 1.21 - Convert any string of blanks into minimum number of tabs and spaces
  2. #include <stdio.h>
  3.  
  4. #define TAB_STOP  8     /* the number of columns between each tab stop */
  5. #define SPACE     ' '   /* set to visible character (ex. '-') for debug */
  6. #define BLANK     1     /* inside a blank space */
  7. #define NOT_BLANK 0     /* inside visible characters (not blank) */
  8.  
  9. /* replace all strings of blanks in the input with the minimum
  10.  * number of tabs and spaces to achieve the same spacing */
  11. int main()
  12. {
  13.     int c, col, tab_length, blank_length, potential_tab_length, state;
  14.    
  15.     col = 0;                        /* number of columns covered */
  16.     blank_length = 0;               /* length of the current string of blanks (in cols) */
  17.     while((c = getchar()) != EOF)
  18.     {
  19.         if(c == '\n') col = -1;     /* reset column count at end of each line */
  20.        
  21.         if(c == ' ' || c == '\t')   /* set state to blank whenever a blank character is encountered */
  22.         {
  23.             state = BLANK;
  24.         }
  25.         else                        /* if a non-blank character is encountered */
  26.         {
  27.             ++col;
  28.             if(state == BLANK)      /* if previously blank, show minimum number of tabs and spaces to cover blank space */
  29.             {
  30.                 potential_tab_length = (TAB_STOP - ((col - blank_length - 1) % TAB_STOP));
  31.                 while(blank_length - potential_tab_length >= 0)
  32.                 {
  33.                     putchar('\t');
  34.                     blank_length = blank_length - potential_tab_length;
  35.                     potential_tab_length = (TAB_STOP - ((col - blank_length - 1) % TAB_STOP));
  36.                 }
  37.                
  38.                 while(blank_length != 0)
  39.                 {
  40.                     putchar(SPACE);
  41.                     --blank_length;
  42.                 }
  43.                
  44.                 blank_length = 0;
  45.                 state = NOT_BLANK;
  46.             }
  47.             putchar(c);
  48.         }
  49.        
  50.         if(state == BLANK)          /* update blank_length and col counters */
  51.         {
  52.             if(c == ' ')
  53.             {
  54.                 ++blank_length;
  55.                 ++col;
  56.             }
  57.             else
  58.             {
  59.                 tab_length = (TAB_STOP - (col % TAB_STOP));
  60.                 blank_length = blank_length + tab_length;
  61.                 col = col + tab_length;
  62.             }
  63.         }
  64.     }
  65.     return 0;
  66. }