Question
Can someone help me fix up my code? I am trying to make a code that when I type in a sentence, the program counts
Can someone help me fix up my code? I am trying to make a code that when I type in a sentence, the program counts and displays how many vowels, constants, digits, and white spaces there are. When I run this code and type a sentence down, it shows that the values of the things I am looking for is zero.
This is the code I have so far.
#include
using namespace std;
int main() {
char input;
cin >> input;
char str[] = {input};
int vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
for(int i = 0; str[input]!='\0'; ++i) {
if(str[input]=='a' || str[input]=='e' || str[input]=='i' ||
str[input]=='o' || str[input]=='u' || str[input]=='A' ||
str[input]=='E' || str[input]=='I' || str[input]=='O' ||
str[input]=='U') {
++vowels;
} else if((str[input]>='a'&& str[input]<='z') || (str[input]>='A'&& str[input]<='Z')) {
++consonants;
} else if(str[input]>='0' && str[input]<='9') {
++digits;
} else if (str[input]==' ') {
++spaces;
}
}
cout << "The string is: " << str << endl;
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
cout << "Digits: " << digits << endl;
cout << "White spaces: " << spaces << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started