The Program in C to count all the vowels, consonants, digits, spaces, special symbols from a given text typed by the user is given below:
#include <stdio.h>
int main()
{
char text[1000];
int vowels = 0, consonants = 0, digits = 0, spaces = 0, special = 0;
printf("Enter some text: ");
scanf("%[^\n]s",text);
for (int i = 0; text[i] != '\0'; i++)
{
if (text[i] >= 'A' && text[i] <= 'Z' || text[i] >= 'a' && text[i] <= 'z') {
if(text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || text[i] == 'u' ||
text[i] == 'A' || text[i] == 'E' || text[i] == 'I' || text[i] == 'O' || text[i] == 'U') {
vowels++;
}
else {
consonants++;
}
}
else if (text[i] >= '0' && text[i] <= '9') {
digits++;
}
else if (text[i] == ' ') {
spaces++;
}
else {
special++;
}
}
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
printf("Digits: %d\n", digits);
printf("Spaces: %d\n", spaces);
printf("Special symbols: %d\n", special);
return 0;
}
Output:
Enter some text: 34r&A?
Vowels: 1
Consonants: 1
Digits: 2
Spaces: 0
Special symbols: 2
Pro-Tips💡
This code is a simplified version of the previous code, where I have removed the repetitive checks for vowels and added a check for alphabets before checking for vowels, also scanf is used to take input instead of gets() which is a deprecated function.
Also, I have combined the if conditions to check for vowels and consonants into one,
and removed the unnecessary else statements.
This reduces the number of checks the program needs to make, making it more efficient.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.