/* * Program to count characters read from stdin. * Also counts how many are letters (as an example of doing something * with each character). */ #include #include /* has EXIT_SUCCESS, EXIT_FAILURE */ #include int main(int argc, char* argv[]) { int count = 0; int letter_count = 0; int ch; /* input character -- NOTE that it is int not char */ printf("enter some text, multiple lines okay, control-D to end\n"); /* slightly cryptic but C-idiomatic way to read to end of file */ while ((ch = getchar()) != EOF) { ++count; if (isalpha(ch)) ++letter_count; } printf("%d chars, %d letters\n", count, letter_count); return EXIT_SUCCESS; }