C Program to Count the Number of Vowels, Consonants, and so on
Example: Program to count vowels, consonants, etc.
#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
return 0;
}
Output
Enter a line of string: adfslkj34 34lkj343 34lk Vowels: 1 Consonants: 11 Digits: 9 White spaces: 2
This program takes string input from the user and stores it in a variable line.
Initially, the variables vowels, consonants, digits, and spaces are initialized to 0.
When the vowel character is found, the vowel variable is incremented by 1. Similarly, consonants, digits, and spaces are incremented when these characters are found.
Finally, the count is displayed on the screen.
C program that counts the number of vowels, consonants, digits, and spaces in a given string:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int vowels = 0, consonants = 0, digits = 0, spaces = 0, i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; i < strlen(str); ++i) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
++vowels;
} else if (ch >= 'a' && ch <= 'z') {
++consonants;
} else if (isdigit(ch)) {
++digits;
} else if (ch == ' ') {
++spaces;
}
}
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
printf("Digits: %d\n", digits);
printf("Spaces: %d\n", spaces);
return 0;
}
This program uses a for
loop to iterate through each character in the given string. It then uses tolower()
to convert the character to lowercase (so that it can be compared to lowercase vowel letters). If the character is a vowel, it increments the vowels
counter. If the character is a consonant, it increments the consonants
counter. If the character is a digit, it increments the digits
counter. If the character is a space, it increments the spaces
counter. Finally, it prints out the counts for each category.
a step-by-step explanation of a C program that counts the number of vowels and consonants in a given string:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int vowels = 0, consonants = 0, i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // reads a line of text from the standard input stream
for (i = 0; i < strlen(str); ++i) { // loop through each character in the string
char ch = tolower(str[i]); // convert the character to lowercase to simplify the checking process
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { // check if the character is a vowel
++vowels; // increment the vowel counter
} else if (ch >= 'a' && ch <= 'z') { // check if the character is a consonant
++consonants; // increment the consonant counter
}
}
printf("Number of vowels: %d\n", vowels); // print the number of vowels
printf("Number of consonants: %d\n", consonants); // print the number of consonants
return 0; // indicate successful completion of the program
}
Let’s break it down step-by-step:
We include the necessary header files:
stdio.h
for input/output functions,string.h
for string handling functions, andctype.h
for character handling functions.We declare a character array
str
of size100
to hold the input string, and integer variablesvowels
andconsonants
to keep track of the number of vowels and consonants, respectively.We prompt the user to enter a string using
printf()
, and read a line of text from the standard input stream usingfgets()
, which takes in three arguments: the character array to store the input, the size of the array, and the input stream (in this case,stdin
for standard input).We use a
for
loop to iterate through each character in the string. The loop runs fromi = 0
toi < strlen(str)
, wherestrlen(str)
returns the length of the string (i.e., the number of characters in the string).Inside the loop, we use
tolower()
to convert the current character to lowercase, which simplifies the checking process.We use an
if
statement to check if the current character is a vowel. If it is, we increment thevowels
counter.We use an
else if
statement to check if the current character is a consonant. If it is, we increment theconsonants
counter.Once the loop is complete, we use
printf()
to print the number of vowels and consonants.Finally, we return
0
to indicate successful completion of the program.