Vowel Consonant Digit Space Special Character Count in C

Count in c programming. Here we will write a C program to count the number of vowels, consonants, spaces, and digits, and many more in a string.

Program description:- Write a C program to count the numbers of vowels, consonants, digits, white spaces, special characters, and words in a given line of text.

#include<stdio.h>
#include<ctype.h>
int main()
{
   char s[100];
   int i, vowel, conso, digit, space, special, word;
   vowel=conso=digit=space=special=word=0;

   printf("Enter a string:\n");
   scanf("%[^\n]", s);

   // conveting all uppercase into lowercase
   for(i=0; s[i]!='\0'; i++)
   s[i]=tolower(s[i]);
   for(i=0; s[i]!='\0';i++)
   {
      if(s[i]>='a'&&s[i]<='z')
      {
          if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
          vowel++;
          else conso++;
      }
      else if(s[i]==' '||s[i]=='\t'||s[i]=='\n')
      {
          space++;
          word++;
      }
      else if(s[i]>='0'&&s[i]<='9') digit++;
      else special++;
   }

   printf("\nVowel counts = %d\n",vowel);
   printf("Consonant counts = %d\n",conso);
   printf("Digit counts = %d\n",digit);
   printf("Special character counts = %d\n",special);
   printf("Space counts = %d\n",space);
   printf("Word counts = %d\n",word+1);

   return 0;
}

Output:-

Enter a string:
Hello, Bro how are you? On 23rd April there was an Elephant at big circle.

Vowel counts = 22
Consonant counts = 33
Digit counts = 2
Special character counts = 3
Space counts = 14
Word counts = 15

In this program count in C programming, we convert the text into lowercase. So, there is an uppercase character than it will be converted into lowercase, remaining all other characters will remain the same. We did this thing because to easily search the vowels and consonants. We can do the same without converting into lowercase but in that case, we need to take both upper and lowercase to check alphabets and vowles.

If any character is in between ‘a’ to ‘z’ then it is an alphabet. When any character is alphabet then it can be vowel or consonant. To check character is a vowel or not use compare it with a,e,i,o and u. If both characters are equal then it is vowel otherwise it is consonant.

If any character is not an alphabet and it is in between ‘0’ to ‘9’ (including) then character is a digit. if the character is space or tab or newline then the variable space will be incremented. When all the above conditions become false then character is a special character.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *