Solution for Exercise 13-3
ex1303
#include <stdio.h> #include <ctype.h> int main() { char phrase[] = "When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; int index; int alpha,blank,lower,punct,upper; alpha = blank = punct = lower = upper = 0; /* gather data */ index = 0; while(phrase[index]) { if(isalpha(phrase[index])) alpha++; if(isblank(phrase[index])) blank++; if(ispunct(phrase[index])) punct++; if(islower(phrase[index])) lower++; if(isupper(phrase[index])) upper++; index++; } /* print results */ printf("\"%s\"\n",phrase); puts("Statistics:"); printf("%d alphabetic characters\n",alpha); printf("%d blanks\n",blank); printf("%d punctuation symbols\n",punct); printf("%d lower case letters\n",lower); printf("%d upper case letters\n",upper); printf("%d total characters\n",index); return(0); }
Notes
* Because the index
variable keeps track of the text's length, you can just use it to display the overall tally.
* You could have used the isprint() function as another text, but index
is already doing the job.
Copyright © 1997-2024 by QPBC.
All rights reserved