Print vowels,consonent,captital,small and special character using String in C


This is a C program that takes a sentence as input from the user and counts the number of vowels, consonants, capital letters, small letters, and special characters in the sentence.

The program declares 5 variables, small, cap, vowel, cons, and spl, to store the count of each category of characters. The function gets is used to read the input sentence from the user and store it in an array of characters named a.

The for loop iterates over the elements of the a array until it encounters a null character ('\0'), which marks the end of the string. The code inside the loop checks the ASCII value of each character in the string and increments the appropriate counter based on the following conditions:

  • If the character is between 97 and 122 (inclusive), it is a small letter and small counter is incremented. If the character is a vowel (a, e, i, o, or u), the vowel counter is incremented. Otherwise, the cons counter is incremented.
  • If the character is between 65 and 90 (inclusive), it is a capital letter and cap counter is incremented. If the character is a vowel (A, E, I, O, or U), the vowel counter is incremented. Otherwise, the cons counter is incremented.
  • If the character is not a small or capital letter, it is a special character, and the spl counter is incremented.

Source Code

#include<stdio.h>
int main()
{
  int i,small=0,cap=0,vowel=0,cons=0,spl=0;
  char a[100];
  printf("\nEnter the Sentence:");
  gets(a);
  for(i=0;a[i]!='\0';i++)
  {
    if(a[i]>=97&&a[i]<=122)
    {
        small++;
        if(a[i]==97||a[i]==101||a[i]==105||a[i]==111||a[i]==117)
        {
            vowel++;
        }
        else
        {
            cons++;
        }
    }
    else if(a[i]>=65&&a[i]<=90)
    {
      cap++;
	  
      if(a[i]==65||a[i]==69||a[i]==73||a[i]==79||a[i]==85)
      {
        vowel++;
      }
      else
      {
        cons++;
      }
    }
    else
    {
       spl++;
    }
  }

  printf("\nVowels:%d",vowel);
  printf("\nConsonents:%d",cons);
  printf("\nCapital letters:%d",cap);
  printf("\nSmall letters:%d",small);
  printf("\nSpecial characters:%d",spl);
  return 0;
}
To download raw file Click Here

Output

Enter the Sentence: Tutor Joe's Computer Education is the best education center
Tutor Joe's Computer Education is the best education center

Vowels:22
Consonents:28
Capital letters:4
Small letters:46
Special characters:9

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C