home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch2 / tabs.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  58 lines

  1. /*                         tabs.c
  2.  *
  3.  *   Synopsis  - A filter that processes its input by replacing 
  4.  *               every tab character and ^A character with
  5.  *               TABSTOP spaces.
  6.  *
  7.  *   Objective - Illustrates the use of a logical operator in a 
  8.  *               structured program that contains a useful tool.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13.  
  14. /* Constant Definitions */
  15. #define TAB      '\t'
  16. #define ALTTAB     1        /* CTRL-A in ASCII */
  17. #define TABSTOP    5
  18. #define SPACE     ' '       /* the ASCII space character */
  19.  
  20. /* Function Prototypes */
  21. void processtabs( int tabstop, int character );
  22. /*   PRECONDITION:  tabstop is the number of characters to display.
  23.  *                  character is the integer ASCII value of the 
  24.  *                  character to use for replacing tabs and CTRL-As.
  25.  *
  26.  *   POSTCONDITION: tabstop occurrences of character are displayed on 
  27.  *                  the terminal screen.
  28.  */
  29.  
  30. int main( void )
  31. {
  32.      int iochar;
  33.  
  34.      /*  The following while loop processes each input
  35.       *  character.  If the character is one of the two
  36.       *  designated tab characters, the function processtabs()
  37.       *  is called. Otherwise, the character is output with
  38.       *  putchar().
  39.       */
  40.  
  41.      while ( ( iochar = getchar() ) != EOF )
  42.           if ( ( iochar == TAB ) || ( iochar == ALTTAB ) )
  43.                processtabs( TABSTOP, SPACE );
  44.           else
  45.                putchar( iochar );
  46.      return 0;
  47. }
  48.  
  49. /*******************************processtabs()*******************/
  50.  
  51. void processtabs( int tabstop, int character )
  52. {
  53.      int colcount = 0;
  54.  
  55.      while ( colcount++ < tabstop )
  56.           putchar( character );
  57. }
  58.