lynx@qantel.UUCP (D.N. Lynx Crowe@ex2207) (10/23/86)
The character classification program "ctype.c" recently posted to the net has a bug in it: the headings for the columns are in the wrong order. I decided that what I wanted was a complete count of all characters, not just those below octal 200, so I put together the following, (derived from atype.c and ctype.c). ---------------------------------------------------------- D.N. Lynx Crowe {dual, hplabs, lll-crg, ptsfa}!qantel!lynx ---------------------------------------------------------- "Just say 'No' -- to Government" ---------------------------------------------------------- ============= cut here and put into a file called ccount.c =============== /* ============================================================================= ccount.c -- find numbers of different types of characters in a file Version 1 -- 1986-10-23 -- D.N. Lynx Crowe ============================================================================= */ #include <stdio.h> #include <ctype.h> char *maptable[32][8] = { "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", "bs ", "ht ", "nl ", "vt ", "np ", "cr ", "so ", "si ", "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us ", "sp ", " ! ", " \" "," # ", " $ ", " % ", " & ", " ' ", " ( ", " ) ", " * ", " + ", " , ", " - ", " . ", " / ", " 0 ", " 1 ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ", " 8 ", " 9 ", " : ", " ; ", " < ", " = ", " > ", " ? ", " @ ", " A ", " B ", " C ", " D ", " E ", " F ", " G ", " H ", " I ", " J ", " K ", " L ", " M ", " N ", " O ", " P ", " Q ", " R ", " S ", " T ", " U ", " V ", " W ", " X ", " Y ", " Z ", " [ ", " \\ ", " ] ", " ^ ", " _ ", " ` ", " a ", " b ", " c ", " d ", " e ", " f ", " g ", " h ", " i ", " j ", " k ", " l ", " m ", " n ", " o ", " p ", " q ", " r ", " s ", " t ", " u ", " v ", " w ", " x ", " y ", " z ", " { ", " | ", " } ", " ~ ", "del", "200", "201", "202", "203", "204", "205", "206", "207", "210", "211", "212", "213", "214", "215", "216", "217", "220", "221", "222", "223", "224", "225", "226", "227", "230", "231", "232", "233", "234", "235", "236", "237", "240", "241", "242", "243", "244", "245", "246", "247", "250", "251", "252", "253", "254", "255", "256", "257", "260", "261", "262", "263", "264", "265", "266", "267", "270", "271", "272", "273", "274", "275", "276", "277", "300", "301", "302", "303", "304", "305", "306", "307", "310", "311", "312", "313", "314", "315", "316", "317", "320", "321", "322", "323", "324", "325", "326", "327", "330", "331", "332", "333", "334", "335", "336", "337", "340", "341", "342", "343", "344", "345", "346", "347", "350", "351", "352", "353", "354", "355", "356", "357", "360", "361", "362", "363", "364", "365", "366", "367", "370", "371", "372", "373", "374", "375", "376", "377" } ; FILE *fp; FILE *fopen(); void tally(); #define NASCII 0 #define NCNTRL 1 #define NPRINT 2 #define NSPACE 3 #define NPUNCT 4 #define NALNUM 5 #define NDIGIT 6 #define NALPHA 7 #define NUPPER 8 #define NLOWER 9 #define NCLASS 10 long class[NCLASS]; long tc; int count[8][32]; /* ============================================================================= main -- driver for ccount.c ============================================================================= */ main(argc, argv) int argc; char *argv[]; { int i, j, k; for( j = 0; j < NCLASS; j++) class[j] = 0L; tc = 0L; if( argc == 1) { fp = stdin; tally(fp); } else { for (i = 1; i < argc; i++) { if ((fp = fopen(argv[i],"r")) == NULL) { (void) fprintf(stderr, "ccount: can't open %s\n", argv[i]); continue; } tally(fp); (void) fclose(fp); } } for(k = 0; k < 16; k++) { for(j = 0; j < 8; j++) (void) printf("%5d %s",count[j][k],maptable[k][j]); (void) printf("\n"); } (void) printf("\n"); for(k = 16; k < 32; k++) { for(j = 0; j < 8; j++) (void) printf("%5d %s",count[j][k],maptable[k][j]); (void) printf("\n"); } (void) printf("\n\nTotal characters processed: %ld\n\n", tc); (void) printf("ascii\tcntrl\tprint\tspace\tpunct\talnum\tdigit\talpha\tupper\tlower\n"); for ( j = 0; j <NCLASS; j++) (void) printf("%ld\t",class[j]); (void) printf("\n"); } /* ============================================================================= tally(filep) -- tally the types of characters in a file ============================================================================= */ void tally(filep) FILE *filep; { int c; while((c = getc(filep)) != EOF){ count[ c % 8 ][ c / 8 ]++; tc++; if(isascii(c) == 0) continue; class[NASCII]++; if(iscntrl(c)) class[NCNTRL]++; if(isprint(c)) class[NPRINT]++; if(isspace(c)) class[NSPACE]++; if(ispunct(c)) class[NPUNCT]++; if(isalnum(c)) class[NALNUM]++; if(isdigit(c)) class[NDIGIT]++; if(isalpha(c)) { class[NALPHA]++; if(isupper(c)) class[NUPPER]++; if(islower(c)) class[NLOWER]++; } } } -- ---------------------------------------------------------- D.N. Lynx Crowe {dual, hplabs, lll-crg, ptsfa}!qantel!lynx ---------------------------------------------------------- "Just say 'No' -- to Government" ----------------------------------------------------------