C Program to Count number of words,digits,vowels using pointers

Write a program to count the number of words, lines and characters in a text

/*Program finding number of words, blank spaces, special symbols, digits, vowels using pointers*/
#define low 1
#define high 0

   int NumOfBlank, NumOfWord, NumOfDigit, NumOfVowl, NumOfSpecial, pos = high;
   char *str;
   NumOfBlank= NumOfWord = NumOfDigit = NumOfVowl = NumOfSpecial = 0;

   printf("Enter string :\n ");

 /*accept string using gets(). We are not using scanf() to accept string because scanf() will accept string only upto whitespace*/

   while (*str != '\0') 

 /*accepting string using character pointer. NumOfWord we are checking each character using character   pointer and in each loop we are incrementing the character pointer*/
 /*Whenever first space is encountered then number of space counter is incremented by one*/
      if (*str == ' ')
         // count number of blank spaces.
         pos = high;
      } else if (pos == high)
         // count number of words.
         pos = low;

      if (isdigit(*str))          /* count number of digits. */
      if (isalpha(*str))         /* count number of vowels */
         switch (*str) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':

      /* count number of special characters */
      if (!isdigit(*str) && !isalpha(*str))

   printf("Number of words  %d\n", NumOfWord);
   printf("Number of spaces %d\n",NumOfBlank);
   printf("Number of vowels %d\n", NumOfVowl);
   printf("Number of digits %d\n", NumOfDigit);
   printf("Number of special characters %d\n", NumOfSpecial);


Related Posts