I have written the following code to count the number of words, uppercase/lowercase letters, punctuation, and digits. The only problem I am having is counting the last word. Any suggestions? Do I need something after the loop?
Thanks.Code:#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define SIZE 80 int main(void) { char s[SIZE]; int sl; int p; int QW=0; int QUL=0; int QLL=0; int QP=0; int QD=0; printf("This program will request and store a string of characters\n"); printf("and then report:\n"); printf(" Number of Words.\n"); printf(" Number of uppercase letters.\n"); printf(" Number of lowercase letters.\n"); printf(" Number of punctuation characters.\n"); printf(" Number of digits.\n\n"); fflush(stdin); /* Flush Keyboard Buffer */ printf("On the line below, enter the string that should be analyzed:\n"); printf("\n> "); fgets(s, SIZE, stdin); if(s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = '\0'; sl = strlen(s); for(p=0; p < sl; p++) if(s[p] != '\0') { if(isupper(s[p])) QUL = QUL + 1; else if(islower(s[p])) QLL = QLL + 1; else if(isdigit(s[p])) QD = QD + 1; else if(ispunct(s[p])) QP = QP + 1; else if(isspace(s[p])) QW = QW + 1; } printf("\nRESULTS:\n\n"); printf("Number of words: %d\n", QW); printf("Number of uppercase letters: %d\n", QUL); printf("Number of lowercase letters: %d\n", QLL); printf("Number of punctuation characters: %d\n", QP); printf("Number of digits: %d\n", QD); fflush(stdin); getchar(); return(0); }



LinkBack URL
About LinkBacks


