Program in C & C++ to count the number of vowels and consonants in a string Using switch case.
C
CPP
#include<stdio.h> #include<ctype.h> int main() { char sptr[40]; int v_count=0,c_count=0,i; printf("Enter string:-"); gets(sptr); i=0; while(sptr[i]!='\0') { switch(toupper(sptr[i])) { case 'A': case 'E': case 'I': case 'O': case 'U':v_count++; break; default: c_count++; } i++; } printf("\nNumber of vowels = %d",v_count); printf("\nNumber of consonants = %d",c_count); return 0; }
#include<iostream> #include<ctype.h> using namespace std; int main() { char sptr[40]; int v_count=0,c_count=0,i; cout<<"Enter string:-"; cin.getline(sptr,40); i=0; while(sptr[i]!='\0') { switch(toupper(sptr[i])) { case 'A': case 'E': case 'I': case 'O': case 'U':v_count++; break; default: c_count++; } i++; } cout<<"\nNumber of vowels = "<<v_count; cout<<"\nNumber of consonants = "<<c_count; return 0; }
Output:-
Related programs
0 Comments