Program in C and C++ to print how many vowels, consonants and words are present in given sentence.
C
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { char *str; str=malloc(100); printf("Enter sentence :- "); gets(str); int i,vowels=0,consonants=0,words=1; i=0; while(str[i]!='\0') { if(str[i]>=65&&str[i]<=90||str[i]>=97&&str[i]<=122||str[i]==' ') { switch(str[i]) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': vowels++; break; case ' ': words++; break; default: consonants++; } } i++; } printf("\nTotal number of characters = %d",i); printf("\nTotal number of vowels = %d",vowels); printf("\nTotal number of consonants = %d",consonants); printf("\nTotal number of words = %d",words); free(str); getch(); }
CPP
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int main() { char *str; str=(char*)malloc(100); cout<<"Enter sentence :- "; cin.getline(str,99); int i,vowels=0,consonants=0,words=1; i=0; while(str[i]!='\0') { if(str[i]>=65&&str[i]<=90||str[i]>=97&&str[i]<=122||str[i]==' ') { switch(str[i]) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': vowels++; break; case ' ': words++; break; default: consonants++; } } i++; } cout<<"\nTotal number of characters = "<<i; cout<<"\nTotal number of vowels = "<<vowels; cout<<"\nTotal number of consonants = "<<consonants; cout<<"\nTotal number of words = "<<words; free(str); getch(); }
Output :-
0 Comments