In this article, we will discuss the concept of the C code: count vowels, consonants, digits, special characters and spaces
In this post, we are going to learn how to count the total number of Vowels, consonants, digits, special character and Space of the given string in C programming language
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, special characters and Spaces of the given string using for loop in C programing language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { char str[100];//declare and initialize char array int i; int digits=0,spaces=0,spe_Char=0;//display and initialize variables int vCount=0,cCount=0; printf("Please enter the string \n"); gets(str);//stored the string entered by user for(i=0; str[i] != '\0'; i++){ if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u' || // check vowels str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' ){ vCount++; } else if( (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ //check consonants cCount++; } else if(str[i]>='0' && str[i]<='9') //check digits { digits++; } else if(str[i]==' '){ //check spaces spaces++; } else{ spe_Char++; } } printf("\nTotal Vowels:%d ",vCount); //display total number of vowels printf("\nTotal consonants:%d ",cCount); //display total number of consonants printf("\nTotal digits:%d ",digits); //display total number of digits printf("\nTotal white space :%d ",spaces); //display total number of spaces printf("\nTotal special characters:%d ",(spe_Char)); //display total number of special characters getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string coding tutorials:code4coding.com Total Vowels:11 Total consonants:17 Total digits:1 Total white space:1 Total special characters:2
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, special character and Space of the given string using while loop in C programing language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { char str[100];//declare and initialize char array int i; int digits=0,spaces=0,spe_Char=0;//display and initialize variables int vCount=0,cCount=0; printf("Please enter the string \n"); gets(str);//stored the string entered by user i=0; while(str[i] != '\0'){ 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' ){ vCount++; } else if( (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ cCount++; } else if(str[i]>='0' && str[i]<='9') { digits++; } else if(str[i]==' '){ spaces++; } else{ spe_Char++; } i++; } printf("\nTotal Vowels:%d ",vCount); printf("\nTotal consonants:%d ",cCount); printf("\nTotal digits:%d ",digits); printf("\nTotal white space :%d ",spaces); printf("\nTotal special characters:%d ",(spe_Char)); getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string Java,C,C++,Python:code for coding.c0m Total Vowels:9 Total consonants:17 Total digits:1 Total white space:2 Total special characters:6
Count vowel, consonant, digit, special character and space using do-while loop
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, special character and Space of the given string using do-while loop in C programing language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { char str[100];//declare and initialize char array int i; int digits=0,spaces=0,spe_Char=0;//display and initialize variables int vCount=0,cCount=0; printf("Please enter the string \n"); gets(str);//stored the string entered by user i=0; do{ 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' ){ vCount++; } else if( (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ cCount++; } else if(str[i]>='0' && str[i]<='9') { digits++; } else if(str[i]==' '){ spaces++; } else{ spe_Char++; } i++; } while(str[i] != '\0'); printf("\nTotal Vowels:%d ",vCount); printf("\nTotal consonants:%d ",cCount); printf("\nTotal digits:%d ",digits); printf("\nTotal white space :%d ",spaces); printf("\nTotal special characters:%d ",(spe_Char)); getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string support@cod4coding.c0m email address Total Vowels:12 Total consonants:20 Total digits:1 Total white space:2 Total special characters:2
Approaches
Suggested for you
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…