In this example we will count the number of vowel, Consonant, number & space
#include <iostream>
using namespace std;
int main() {
char line[150];
int vowel, consonant, number, space;
vowel = consonant = number = space = 0;
cout << "Enter a Sentence" << endl;
cin.getline(line, 150); // Use cin.getline to read the input, including spaces
for (int i = 0; line[i] != '\0'; i++) {
// Check for vowels (both uppercase and lowercase)
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' ||
line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U') {
++vowel;
}
// Check for consonants (both uppercase and lowercase)
else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
++consonant;
}
// Check for numbers
else if (line[i] >= '0' && line[i] <= '9') {
++number;
}
// Check for spaces
else if (line[i] == ' ') {
++space;
}
}
cout << "Vowel is = " << vowel << endl;
cout << "Consonant is = " << consonant << endl;
cout << "Number is = " << number << endl;
cout << "Space is = " << space << endl;
return 0;
}
Explanation:
- User Input:
- The program prompts the user to enter a sentence, and the user inputs the string:
Enter a Sentence:
Hello World 123
- This sentence is stored in the
line
array.
- Initialization:
- The program initializes the following variables:
vowel = 0
: To count the number of vowels.consonant = 0
: To count the number of consonants.number = 0
: To count the digits (numbers).space = 0
: To count the number of spaces.
- The program initializes the following variables:
- Loop Through Each Character:
- The
for
loop iterates over each character in the input string and performs the following checks:
- The
Character | Condition | Action Taken |
---|---|---|
H | else if (line[i] >= 'A' && line[i] <= 'Z') | Consonant count increases by 1 |
e | `if (line[i] == ‘a’ | |
l | else if (line[i] >= 'a' && line[i] <= 'z') | Consonant count increases by 1 |
l | else if (line[i] >= 'a' && line[i] <= 'z') | Consonant count increases by 1 |
o | `if (line[i] == ‘a’ | |
else if (line[i] == ' ') | Space count increases by 1 | |
W | else if (line[i] >= 'A' && line[i] <= 'Z') | Consonant count increases by 1 |
o | `if (line[i] == ‘a’ | |
r | else if (line[i] >= 'a' && line[i] <= 'z') | Consonant count increases by 1 |
l | else if (line[i] >= 'a' && line[i] <= 'z') | Consonant count increases by 1 |
d | else if (line[i] >= 'a' && line[i] <= 'z') | Consonant count increases by 1 |
else if (line[i] == ' ') | Space count increases by 1 | |
1 | else if (line[i] >= '0' && line[i] <= '9') | Number count increases by 1 |
2 | else if (line[i] >= '0' && line[i] <= '9') | Number count increases by 1 |
3 | else if (line[i] >= '0' && line[i] <= '9') | Number count increases by 1 |
4. Final Counts:
- After the loop finishes, the following values are computed:
- Vowels: 3 (
e
,o
,o
) - Consonants: 7 (
H
,l
,l
,W
,r
,l
,d
) - Numbers: 3 (
1
,2
,3
) - Spaces: 2 (between
Hello
andWorld
, and betweenWorld
and123
)
- Vowels: 3 (
5. Output:
- The program prints the result:
Vowel is = 3
Consonant is = 7
Number is = 3
Space is = 2