vowel & consonant counting
Mar 8, 2014 at 7:22am UTC
okay am writing a code that reads the number of vowels and consonant...code is running only for counting vowels n not consonants....made separate loops for each,,,,can someone help me count also the consonants coz am using != to vowels to count the consonats...plzzzzzzzzzzzzzzzzz help....any help will be highly appreciated......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout<<"please type in a sentence" <<endl;
getline(cin,input);
int numofvowel=0;
int numofconsonant=0;
for (unsigned int i=0;i<input.length();i++)
{
if (input.at(i)=='a' )
{
numofvowel++;
}
else if (input.at(i)=='e' )
{
numofvowel++;
}
else if (input.at(i)=='i' )
{
numofvowel++;
}
else if (input.at(i)=='o' )
{
numofvowel++;
}
else if (input.at(i)=='u' )
{
numofvowel++;
}
}
for (unsigned int j=0;j<input.length();j++)
{
if (input.at(j)=!'a' )
{
numofconsonant++;
}
else if (input.at(j)=!'e' )
{
numofconsonant++;
}
else if (input.at(j)=!'i' )
{
numofconsonant++;
}
else if (input.at(j)=!'o' )
{
numofconsonant++;
}
else if (input.at(j)=!'u' )
{
numofconsonant++;
}
}
cout<<"number of vowels: " <<numofvowel<<endl;
cout<<"number of consonants:" <<numofconsonant<<endl;
return 0;
}
Mar 8, 2014 at 7:53am UTC
1.Just add an 'else' at the end of first if-else structure:
1 2
else
numofconsonant++;
2.Change second if-else into:
1 2
#define a input.at(j)
if (a!='a' &&a!='e' [and so on])
3.You can also use switch.
Mar 8, 2014 at 8:05am UTC
Mar 8, 2014 at 8:15am UTC
1.Just add an 'else' at the end of first if-else structure:
That wouldn't be correct. A character that is not a vowel is not necessarily a consonant. A space character, for instance, is neither.
Your
#define
suggestion is particularly disturbing.
char & a = input.at(j);
would be more appropriate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <iostream>
#include <string>
using namespace std;
int main()
{
const string vowels("aeiou" );
const string consonants("bcdfghjklmnpqrstvwxyz" );
string input;
cout << "please type in a sentence" << endl;
getline(cin, input);
unsigned nVowels = 0;
unsigned nConsonants = 0;
for (auto ch : input)
{
if (vowels.find(ch) != std::string::npos)
++nVowels;
if (consonants.find(ch) != std::string::npos)
++nConsonants;
}
std::cout << "Vowels: " << nVowels;
std::cout << "\nConsonants: " << nConsonants << '\n' ;
}
Mar 8, 2014 at 8:33am UTC
okay i also took into consideration space chacters as you said cire....and also reduced my code a bit... now i want to knw hw i wud output the number of each vowel for instance: a=?
e=?
i=?
o= ?
u=?
question mark indicating the number of each vowel......thanx for the help guys
awsme
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout<<"please type in a sentence" <<endl;
getline(cin,input);
int numofvowel=0;
int numofconsonant=0;
int numofspace=0;
for (unsigned int i=0;i<input.length();i++)
{
if (input.at(i)=='a' )
{
numofvowel++;
}
else if (input.at(i)=='e' )
{
numofvowel++;
}
else if (input.at(i)=='i' )
{
numofvowel++;
}
else if (input.at(i)=='o' )
{
numofvowel++;
}
else if (input.at(i)=='u' )
{
numofvowel++;
}
else if (input.at(i)==' ' )
{
numofspace--;
}
else numofconsonant++;
}
cout<<"number of vowels: " <<numofvowel<<endl;
cout<<"number of consonants:" <<numofconsonant<<endl;
return 0;
}
Topic archived. No new replies allowed.