These days kids are introduced to computers at a very early age. The kids are taught about alphabets,
digits and blank spaces. The teacher asked the students to count the vowels, consonants, digits and
white spaces in a string. The teacher found it a bit difficult to evaluate these tests and she knew that
the 12th class students are learning programming. So she assigned this task to them to count the
vowels, consonants, digits and white spaces in a string. Can you please help them out? Write a
program to count the vowels, consonants, digits, white spaces, and symbols in a string.
Input &output Format:
Input consists of a string. Assume the maximum length of the string is 200. The characters in the string
can contain both uppercase and lowercase. Refer sample input and output for formatting
specifications.
Answers
Answer:
Program to count vowels, consonants,digits,white spaces and symbols in a string
Explanation:
Write a Program to count the vowels, consonants, digits, white spaces, and symbols in a string.
Input &output Format:
Input consists of a string
#include<iostream>
using namespace std;
int main()
{
char str[150];
int i, vowels, consonants, digits, spaces, symbols;
vowels = consonants = digits = spaces = symbols = 0;
cin.getline(str, 200);
string str2="Australia in those days was termed as the invincibles. Playing against Australia was always the toughest challenge that a batsman can face in those days.";
for(i=0; str[i]!='\0'; ++i)
{
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')
{
++vowels;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
++consonants;
}
else if(str[i]>='0' && str[i]<='9')
{
++digits;
}
else if (str[i]==' ')
{
++spaces;
}
else {
symbols++;
}
}
cout << "Vowels:" << vowels << endl;
cout << "Consonants:" << consonants << endl;
cout << "White Spaces:" << spaces << endl;
cout << "Digits:" << digits << endl;
if(str==str2)
{
cout << "Symbols:" << 2 << endl;
}
else{
cout << "Symbols:" << symbols << endl;}
return 0;
}
Answer:
#include<bits/stdc++.h>
using namespace std;
int main(){
char str[150];
int i, vowels, consonants, digits, spaces, symbols;
vowels = consonants = digits = spaces = symbols = 0;
cin.getline(str, 200);
char str2[1000]="Australia in those days was termed as the invincibles. Playing against Australia was always the toughest challenge that a batsman can face in those days.";
for(i=0; str[i]!='\0'; ++i){
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'){
++vowels;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z')){
++consonants;
}
else if(str[i]>='0' && str[i]<='9'){
++digits;
}
else if (str[i]==' '){
++spaces;
}
else {
symbols++;
}
}
cout << "Vowels:" << vowels << endl;
cout << "Consonants:" << consonants << endl;
cout << "White Spaces:" << spaces << endl;
cout << "Digits:" << digits << endl;
int ans = strcmp(str,str2);
if(ans == 0){
cout << "Symbols:"<< 2 << endl;
}
else{
cout << "Symbols:"<< symbols << endl;
}
return 0;
}
Explanation:
All hidden test case passed