C Program to Count the Number of Vowels & Consonants in a Sentence

This C Program counts the number of vowels & consonants in a sentence. This program is used to find the number of vowels and consonants present in the given sentence. The vowels are ‘a’, ‘e’ , ‘i’, ‘o’, ‘u’, The remaining letters are consonants.

Here is source code of the C program to count the number of vowels & consonants in a sentence. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to read a sentence and count the total number of vowels
  3.  * and consonants in the sentence.
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     char sentence[80];
  10.     int i, vowels = 0, consonants = 0, special = 0;
  11.  
  12.     printf("Enter a sentence \n");
  13.     gets(sentence);
  14.     for (i = 0; sentence[i] != '\0'; i++)
  15.     {
  16.         if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
  17.         'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
  18.         (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
  19.         'I' || sentence[i] == 'O' || sentence[i] == 'U'))
  20.         {
  21.             vowels = vowels + 1;
  22.         }
  23.         else
  24.         {
  25.             consonants = consonants + 1;
  26.         }
  27.         if (sentence[i] =='t' ||sentence[i] =='\0' || sentence[i] ==' ')
  28.         {
  29.             special = special + 1;
  30.         }
  31.     }
  32.     consonants = consonants - special;
  33.     printf("No. of vowels in %s = %d\n", sentence, vowels);
  34.     printf("No. of consonants in %s = %d\n", sentence, consonants);
  35. }


$ cc pgm31.c
$ a.out
Enter a sentence
welcome to sanfoundry
No. of vowels in welcome to sanfoundry = 7
No. of consonants in welcome to sanfoundry = 12

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you liked this C Program, kindly share, recommend or like below!

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Sanfoundry Internships
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry