How to do it…

  1. Create a string array called str to input your sentence. As usual, the last character will be a null character:
char str[255];
  1. Define two variables, ctrV and ctrC:
int  ctrV,ctrC;
  1. Prompt the user to enter a sentence of your choice:
printf("Enter a sentence: ");
  1. Execute the gets function to accept the sentence with blank spaces between the words:
gets(str);
  1. Initialize ctrV and ctrC to 0. The ctrV variable will count the number of vowels in the sentence, while the ctrC variable will count the number of consonants in the sentence:
ctrV=ctrC=0;
  1. Execute a while loop to access each letter of the sentence one, by one until the null character in the sentence is reached.
  2. Execute an if block to check whether the letters are uppercase or lowercase, using ASCII values. This also confirms that the accessed character is not a white space, special character or symbol, or number. 
  3. Once that's done, execute a nested if block to check whether the letter is a lowercase or uppercase vowel, and wait for the while loop to terminate:
while(str[i]!='\0')
{
if((str[i] >=65 && str[i]<=90) || (str[i] >=97 && str[i]<=122))
{
if(str[i]=='A' ||str[i]=='E' ||str[i]=='I' ||str[i]=='O'
||str[i]=='U' ||str[i]=='a' ||str[i]=='e' ||str[i]=='i'
||str[i]=='o'||str[i]=='u')
ctrV++;
else
ctrC++;
}
i++;
}

The countvowelsandcons.c program for counting vowels and consonants in a string is as follows:

#include <stdio.h>
void main()
{
char str[255];
int ctrV,ctrC,i;
printf("Enter a sentence: ");
gets(str);
ctrV=ctrC=i=0;
while(str[i]!='\0')
{
if((str[i] >=65 && str[i]<=90) || (str[i] >=97 && str[i]<=122))
{
if(str[i]=='A' ||str[i]=='E' ||str[i]=='I' ||str[i]=='O'
||str[i]=='U' ||str[i]=='a' ||str[i]=='e' ||str[i]=='i'
||str[i]=='o'||str[i]=='u')
ctrV++;
else
ctrC++;
}
i++;
}
printf("Number of vowels are : %d\nNumber of consonants are :
%d\n",ctrV,ctrC);
}

Now, let's go behind the scenes to understand the code better.