Printing out number of vowels found in a string

I'm trying to write a program where the user inputs a string and the number of words in the string and number of vowels is printed out. I already got the wordCount() function working but I'm stuck on the vowelCounter(). I've tweaked a few things so it might have even more errors but what I tried doing was to have a counter that increases whenever a vowel was found in the string but when I run it, it prints a completely wrong number between 0-4. Initially, a size parameter was never even mentioned but I thought it was needed for the for() statement in vowelCounter().

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 <cstring>
using namespace std;

int wordCount(string str1);
int vowelCounter(string str1, int size);

int main()
{

	char str1[200];

	cout << "Enter the string" << endl;
	cin >> str1;

	int size = strlen(str1);

	cin.getline(str1, size);                         // takes in string


	cout << "Number of words: " << wordCount(str1) << '\n' << "Number of vowels: " << vowelCounter(str1, size) << '\n' << "Number of numerals: " << endl;

}

int wordCount(string str1)
{
	int numWords = 0;
	for (int i = 0; str1[i] != 0; i++)        // goes through the string
	{
		if (str1[i] == ' ')                   // counts a word for each space
		{
			numWords++;
		}
	}

	return numWords + 1;                    // add one because there is a word after the last space
}

int vowelCounter(string str1, int size)
{
	int numVowels = 0;

	for (int i = 0; i < size; i++)
	{
		if (str1[i] == 'a' || str1[i] == 'e' || str1[i] == 'i' || str1[i] == 'o' || str1[i] == 'u' || str1[i] == 'A' || str1[i] == 'E' || str1[i] == 'I' || str1[i] == 'O' || str1[i] == 'U')
		{
			numVowels++;
		}
	}
	return numVowels;
}
Just use strings. They know their own size.

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
#include <iostream>
#include <cstring>
using namespace std;

int wordCount(string str1);
int vowelCounter(string str1);

int main()
{
	string str1; // NOT CHAR ARRAY - STRING

	cout << "Enter the string" << endl;
        std::getline(std::cin, str1);

	cout << "Number of words: " << wordCount(str1) << '\n' << "Number of vowels: " << vowelCounter(str1) << '\n' << "Number of numerals: " << endl;

}

int wordCount(string str1)
{
	int numWords = 0;
	for (int i = 0; i<str1.size() ; i++)      // string knows its own size
	{
		if (str1[i] == ' ')                   // counts a word for each space
		{
			numWords++;
		}
	}

	return numWords + 1;                    // add one because there is a word after the last space
}

int vowelCounter(string str1)
{
	int numVowels = 0;

	for (int i = 0; i < str1.size() ; i++) // string knows its own size
	{
		if (str1[i] == 'a' || str1[i] == 'e' || str1[i] == 'i' || str1[i] == 'o' || str1[i] == 'u' || str1[i] == 'A' || str1[i] == 'E' || str1[i] == 'I' || str1[i] == 'O' || str1[i] == 'U')
		{
			numVowels++;
		}
	}
	return numVowels;
}
Last edited on
1. Why are you using both cin >> str1; and cin.getline(str1, size); ?

2. Why are you mixing C-strings and std::strings in the same code?

If you use std::string you should #include the header <string>.
Also if you use string, there is no need to pass a separate size value to your functions. You work around that in function wordCount() by testing for the null terminator. It would be better to use the string size() member function.
i.e. instead of
 
    for (int i = 0; str1[i] != 0; i++)

you would have
 
for (unsigned int i = 0; i < str1.size(); i++)


The value of size, determined by strlen() at line 16 refers to the string entered at line 14. But the getline at line 18 should use 200, or better, sizeof(str1) which will give the array size in bytes. Best of all, get rid of the array, use a std::string, and do this:
1
2
    string str1;
    getline(cin, str1);


Sorry my comments are a bit disjointed - but so too is the code.
Topic archived. No new replies allowed.