Count vowels and constants

I have to create a program that will count the number of vowels and consonants in a string

I need to create 2 functions:
1. a value returning function hat accepts a string as an argument, and returns the number of vowels (including y)
2. a function that accepts a string as an argument, and returns the number of consonants

Example:
The chicken crossed the road

Vowels: 8
Constants: 16

Thank you in advanced for the help!
could you show us what you did?
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
int vowels, constants;
char answer = 'y';
while (answer=='y' || answer == 'Y')
{
cout<< "Enter 'y' to continue, anything else to exit"<<endl;
cin>> answer;
return 0;
}
In pseudo:

get string
pass string to function which counts vowels
return number of vowels
pass string to function which counts consonants
return number of consonants
display number of vowels
display number of consonants
end
hope this function helps:
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
void count()
{
cout<<"enter the string: ";
char a[50];
gets(a);
int vow=0,con=0;
for(int i=0;a[i]!='\0';i++)
{
if(a[i]>='a'&& a[i]<='z' || a[i]>='A' && a[i]<'Z')
{
switch(a[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u' :
case 'U':
vow++;//fallthrough all the way :D
break;
default://those which are not vowels are consoonants
con++;
break;
}
}

}

cout<<"Vowels: "<<vow<<"\nconsonants: "<<con;
}
I developed this program to test for a vowel.

Although it is not quite the same, I think you could get some ideas from this.

I got max score of 100 on it.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/********************************************************************************
**  Author	:  Donald "Chris" Moore
**  Date	:  2 April 2015
**  File	:  Assign-7-Lab-final.cpp
**  Assignment	:  Write a program that prompts the user to input a string.  
**		:  The program then uses the function substr to remove all of 
**		:  the vowels from the string.  For example, if str = “There”, 
**		:  then after removing all of the vowels, str = “Thr”.  After 
**		:  removing all of the vowels, output the string.  Your program
**		:  must contain a function to remove all of the vowels and a
**		:  function to determine whether a character is a vowel.
**  Input from  :  Console
**  Output to	:  Console
********************************************************************************/

#include <iostream>	//  is required for the use of cin and cout 
#include <string>	//  is required for the use of string type data

using namespace std;

//  Function Prototypes
bool isVowel(char ch);
void removeVowel(string & toTest);
void toContinue();

int main()
{
    string userInput;
    
    cout << "Please enter a string for vowel extraction: ";
    getline(cin, userInput);

    removeVowel(userInput);
    
    cout << "The new string is: ";
    cout << endl << userInput << endl;
    
    toContinue();
return 0;
}  //  End of Main

//******************************************************************************
//    the beginning of 'removeVowel' function called from Main
//******************************************************************************
void removeVowel(string & toTest)
{
    string a;  //  temporary part of string before vowel
    string b;  //  temporary part of string after vowel
    int len = 0;  //  Length of the original string
    int i = 0;    //  loop indicees 
    
    len = toTest.length();
    
    for(i=len-1 ; i >= 0 ; --i )
    {
        if (isVowel(toTest[i]))
        {
            a = toTest.substr(0,i);
            b = toTest.substr(i+1);
            toTest = a + b;                  
        }
    }
}  //  End of removeVowel

//******************************************************************************
//    the beginning of 'isVowel' function called from removeVowel
//******************************************************************************
bool isVowel(char ch)
{
    switch(toupper(ch))
    {
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            return true;
         default:
            return false;
    }
}  //  End of isVowel

//******************************************************************************
//    the beginning of 'toContinue' function
//******************************************************************************
void toContinue()
{
	cout << endl << "Press the \'Enter\' key to continue" << endl;
	cin.ignore();
}	// end toContinue 
Last edited on
Hey steph2015, here is my version to your question. All within the main function, though. You can break down into functions. That should be pretty simple ...

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

int main () 
{
	using namespace std; 
	
	string sentence (" ");
	int vowels(0), consonants(0), spaces(0), others(0);
	
	cout << "Write something... ";
	getline (cin, sentence); // Get user input.
	
	for (int i = 0; i < sentence.size(); i++)
	{
           // Count vowels
	   if ( sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' || sentence[i] == 'u' || 
                sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] == 'I' || sentence[i] == 'O' || sentence[i] == 'U' ) 
                  vowels++;
	
           // Count consonants	
	   else if ( sentence[i] >= 'a' && sentence[i] <= 'z' || sentence[i] >= 'A' && sentence[i] <= 'Z' ) 
                  consonants++;
		
           // Count spaces
	   else if ( sentence[i] == ' ' ) spaces++;
		
           // Count any other characters
	   else others++;
	}
	
        // Display result
	cout << "\n\n\tNumber of vowels: " << vowels
	     << "\n\tNumber of consonants: " << consonants
	     << "\n\n\tNumber of spaces: " << spaces
	     << "\n\tNumber of other characters: " << others
	     << "\n\n\tTotal string length = " << sentence.size();
	
    return 0;
}


Bonus: spaces counter and other character counter... happy coding :)
Topic archived. No new replies allowed.