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

  1. /*          return2.c
  2.  *
  3.  *   Synopsis  - Positions input stream to the character after
  4.  *               '#' or after the end of the input line.
  5.  *
  6.  *   Objective - To illustrate a return statement with an
  7.  *               expression.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. /* Constant Definitions */
  14. #define END_OF_LINE  '\n'
  15. #define DELIMITER    '#'
  16.  
  17. /* Function Prototypes */
  18. int look_for_delimiter( char delimiter, char end_of_line );
  19. /*   PRECONDITION:  delimiter and end_of_line can be any character 
  20.  *                  variables.
  21.  *
  22.  *   POSTCONDITION: Reads a line of input and compares each character 
  23.  *                  to delimiter. Returns 1 when delimiter is found, 
  24.  *                  and 0 otherwise.
  25.  */
  26.  
  27. int main( void )
  28. {
  29.      int found;
  30.  
  31.      printf( "Enter a line of text with a '#'.\n> " );
  32.      found = look_for_delimiter( DELIMITER, END_OF_LINE );  /* Note 1 */
  33.      if ( found )                                           /* Note 2 */
  34.           printf( "found a %c.\n", DELIMITER );
  35.      else
  36.           printf( "read until end of line.\n" );
  37.      return 0;
  38. }
  39.  
  40.  
  41. /*******************************look_for_delimiter()************/
  42.  
  43. int look_for_delimiter( char delimiter, char end_of_line )
  44. {
  45.      int iochar;
  46.  
  47.      while ( ( iochar = getchar() ) != END_OF_LINE ) {
  48.           if ( iochar == DELIMITER )
  49.                return 1;                                    /* Note 3 */
  50.      }
  51.      return 0;                                              /* Note 4 */
  52. }
  53.