home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / version-1-0 / Beta / check.c < prev    next >
C/C++ Source or Header  |  2007-09-04  |  2KB  |  77 lines

  1. // check -- Check text file for problem characters
  2. //
  3. // Harry Porter -- 03/02/04
  4. //
  5. // This program runs though its standard input and looks for any strange characters
  6. // such as TAB, CR, LF, BS, DEL, and non-printable characters.  It will print how many
  7. // occurences of each it finds.
  8. //
  9. // This program will also print the maximum line length.  (TAB, DEL, and BS count
  10. // as only 1 character.)
  11. //
  12.  
  13. #define BS 8
  14. #define CR 13
  15. #define LF 10
  16. #define TAB 9
  17. #define DEL 127
  18.  
  19. #include <stdio.h>
  20.  
  21.  
  22. main () {
  23.   int lineLength = 0,
  24.       maxLineLength = 0,
  25.       countOfBS = 0,
  26.       countOfCR = 0,
  27.       countOfLF = 0,
  28.       countOfTAB = 0,
  29.       countOfDEL = 0,
  30.       countOfOther = 0,
  31.       c;
  32.  
  33.   // Each iteration looks at the next character.
  34.   while (1) {
  35.     c = getchar();
  36.     if (c == EOF) {
  37.       break;
  38.     } else if (c == BS) {
  39.       lineLength++;
  40.       countOfBS++;
  41.     } else if (c == DEL) {
  42.       lineLength++;
  43.       countOfDEL++;
  44.     } else if (c == TAB) {
  45.       lineLength++;
  46.       countOfTAB++;
  47.     } else if (c == LF) {
  48.       if (lineLength > maxLineLength) {
  49.         maxLineLength = lineLength;
  50.       }
  51.       lineLength = 0;
  52.       countOfLF++;
  53.     } else if (c == CR) {
  54.       if (lineLength > maxLineLength) {
  55.         maxLineLength = lineLength;
  56.       }
  57.       lineLength = 0;
  58.       countOfCR++;
  59.     } else if ((c >= ' ') && (c < DEL)) {
  60.       lineLength++;
  61.     } else {
  62.       lineLength++;
  63.       countOfOther++;
  64.     }
  65.   }
  66.  
  67.   // Print statistics.
  68.   printf ("The maximum line length is %d\n", maxLineLength);
  69.   printf ("The number of TAB characters is %d\n", countOfTAB);
  70.   printf ("The number of CR characters is %d\n", countOfCR);
  71.   printf ("The number of LF characters is %d\n", countOfLF);
  72.   printf ("The number of BS characters is %d\n", countOfBS);
  73.   printf ("The number of DEL characters is %d\n", countOfDEL);
  74.   printf ("The number of other non-printable characters is %d\n", countOfOther);
  75.  
  76. }
  77.