Solution for Exercise 13-1
ex1301
#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,punct; alpha = blank = punct = 0; /* gather data */ index = 0; while(phrase[index]) { if(isalpha(phrase[index])) alpha++; if(isblank(phrase[index])) blank++; if(ispunct(phrase[index])) punct++; 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); return(0); }
Notes
* If you copy and paste this text into your editor, ensure that the phrase[]
variable at Line 6 appears as a single line. Do not break it up! I've split the line above so that it fits well on this web page. Otherwise, it would be about three feet long and make the page all ugly.
* It's the index++;
statement at Line 21 that moves the program through each character in the string.
* The printf() statement at Line 26 contains a few escape sequences. The \"
is required to stick a double quote in the output, which quotes the string stored in phrase[]
.
Copyright © 1997-2024 by QPBC.
All rights reserved