Reports the number of words read from console, the number of uppercase letters, the number of lowercase letters, the number of punctuation - C String

C examples for String:char array

Description

Reports the number of words read from console, the number of uppercase letters, the number of lowercase letters, the number of punctuation

Demo Code

#include <stdio.h>  
#include <ctype.h>       // for isspace()    
#include <stdbool.h>     // for bool, true, false            

int main(void) {  
   char c;               // read in character   
   int lowerCaseCount = 0;      
   int upperCaseCount = 0;      
   int digitCount = 0;      
   int wordCount = 0;     
   int punctuationCount = 0; 
   bool inword = false;  // == true if c is in a word    
  
   printf("Enter text to be analyzed (EOF to terminate):\n");  

   while ((c = getchar()) != EOF){  
      if (islower(c))  
           lowerCaseCount++;  //w w w .  ja v a 2 s. c  o  m
      else if (isupper(c))  
           upperCaseCount++;  
      else if (isdigit(c))  
           digitCount++;  
      else if (ispunct(c))  
           punctuationCount++;  

      if (!isspace(c) && !inword) {  
         inword = true;  // starting a new word   
         wordCount++;      // count word            
      }  
      if (isspace(c) && inword)  
         inword = false; // reached end of word  
   }  
   printf("\nwords = %d, lowercase = %d, uppercase = %d, "  
          "digits = %d, punctuation = %d\n",  
           wordCount,lowerCaseCount,upperCaseCount, digitCount, punctuationCount);  
   return 0;  
}

Result


Related Tutorials