Program in C & C++ to count the number of vowels and consonants in a string Using if else statement
C
CPP
#include<stdio.h> #include<ctype.h> int main() { char sptr[40],ch; int v_count=0,c_count=0,i; printf("Enter string:-"); gets(sptr); i=0; while(sptr[i]!='\0') { if(sptr[i]>=65&&sptr[i]<=90||sptr[i]>=97&&sptr[i]<=122) { ch=toupper(sptr[i]); if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') v_count++; else 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],ch; int v_count=0,c_count=0,i; cout<<"Enter string:-"; cin.getline(sptr,40); i=0; while(sptr[i]!='\0') { if(sptr[i]>=65&&sptr[i]<=90||sptr[i]>=97&&sptr[i]<=122) { ch=toupper(sptr[i]); if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') v_count++; else c_count++; } i++; } cout<<"\nNumber of vowels = "<<v_count; cout<<"\nNumber of consonants = "<<c_count; return 0; }
Output:-
Related programs
- Program in C & C++ to count the number of vowels and consonants in a string Using switch case.
- Program in C & C++ to count the number of vowels and consonants in a string Using conditional operator
0 Comments