vowel & consonant counting

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;
}  
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.
Vowel (characters) may be in upper case.
Some characters are neither vowels nor consonants.

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
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    const std::string wovels = "AEIOUaeiou" ;

    std::cout << "please type in a sentence\n" ;
    std::string sentence ;
    std::getline( std::cin, sentence ) ;
    std::cout << sentence << '\n' ;

    int num_vowels = 0 ;
    int num_consonants = 0 ;

    for( char c : sentence ) // http://www.stroustrup.com/C++11FAQ.html#for
    {
        // http://www.cplusplus.com/reference/string/string/find/
        if( wovels.find(c) != std::string::npos ) ++num_vowels ;

        // http://www.cplusplus.com/reference/cctype/isalpha/
        else if( std::isalpha(c) /* || std::isdigit(c) ?? */ ) ++num_consonants ;
    }

    std::cout << "number of vowels: " << num_vowels << '\n'
              << "number of consonants: " << num_consonants << '\n' 
              << "number of characters: " << sentence.size() << '\n' ;
}

http://coliru.stacked-crooked.com/a/0fec283c46a2fe7d
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';
}
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.