/* This program is designed to demonstrate the use of "Character classification and conversion" subroutines. Since we are dealing with characters, it is a natural place to demonstrate the use of getchar subroutine and putchar subroutine from the stdio library.
The program objectives are:
-Read input from "stdin"
-Verify that all characters are ascii and printable
-Convert all uppercase characters to lowercase
-Discard multiple white spaces
-Report statistics regarding the types of characters
The following routines are demonstrated by this example program:
- getchar
- putchar
- isascii (ctype)
- iscntrl (ctype)
- isspace (ctype)
- isalnum (ctype)
- isdigit (ctype)
- isalpha (ctype)
- isupper (ctype)
- islower (ctype)
- ispunct (ctype)
- tolower (conv)
- toascii ( conv)
*/
#include <stdio.h> /* The mandatory include file */ #include <ctype.h> /* Included for character classification
subroutines */
/* The various statistics gathering counters */
int asciicnt, printcnt, punctcnt, uppercnt, lowercnt,
digcnt, alnumcnt, cntrlcnt, spacecnt, totcnt, nonprntcnt,linecnt, tabcnt ;
main() {
int ch ; /* The input character is read in to this */ char c , class_conv() ;
asciicnt=printcnt=punctcnt=uppercnt=lowercnt=digcnt==0; cntrlcnt=spacecnt=totcnt=nonprntcnt=linecnt=tabcnt=0; alnumcnt=0;
while ( (ch =getchar()) != EOF ) {
totcnt++; c = class_conv(ch) ; putchar(c);
} printf("The number lines of of input were %d\n",linecnt); printf(" The character wise breakdown follows :\n"); printf(" TOTAL ASCII CNTRL PUNCT ALNUM DIGITS UPPER
LOWER SPACE TABCNT\n");
printf("%5d %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",totcnt,
asciicnt, cntrlcnt, punctcnt, alnumcnt, digcnt, uppercnt,lowercnt, spacecnt, tabcnt );
}
char class_conv(ch) char ch; {
if (isascii(ch)) {
asciicnt++; if ( iscntrl(ch) && ! isspace(ch)) {
nonprntcnt++ ; cntrlcnt++ ; return(' ');
} else if ( isalnum(ch)) {
alnumcnt++; if (isdigit(ch)){
digcnt++; return(ch);
} else if (isalpha(ch)){
if ( isupper(ch) ){
uppercnt++ ; return(tolower(ch));
} else if ( islower(ch) ){
lowercnt++; return(ch);
} else {
/* We should never be in this situation since an alpha character can only be either uppercase or lowercase. */
fprintf(stderr,"Classification error for %c \n",ch); return(NULL);
}
} else if (ispunct(ch) ){
punctcnt++; return(ch);
} else if ( isspace(ch) ){
spacecnt++; if ( ch == '\n' ){ linecnt++; return(ch);
} while ( (ch == '\t' ) || ( ch == ' ' ) ) { if ( ch == '\t' ) tabcnt ++ ; else if ( ch == ' ' ) spacecnt++ ; totcnt++; ch = getchar();
} ungetc(ch,stdin); totcnt--; return(' ');
} else {
/* We should never be in this situation any ASCII character can only belong to one of the above classifications. */ fprintf(stderr,"Classification error for %c \n",ch); return(NULL); }
} else {
fprintf(stdout,"Non Ascii character encountered \n"); return(toascii(ch));
}
}