Write a program that reads a string and outputs the number of times each lowercase vowel appears in it. Your program must contain a function with one of its parameters as a string variable and return the number of times each lowercase vowel appears in it. Also write a program to test your function. (Note that if str is a variable of type string, then str.at(i) returns the character at the ith position. The position of the first character is 0. Also, str.length() returns the length of the str, that is, the number of characters in str.)
So my problem is that I can do this fine without any string variables or functions. How do I solve the problem above using strings? Huh? I'm lost... help is appreciated! That's my code so far... lot of errors, of course.
Also, I have to use a function: void countVowels() with reference parameters, and the input should be: "Summer comes only once a year." (without quotes, obviously)
#include <iostream>
#include <string>
usingnamespace std;
int vowelsNumberof (string, char); //Function Label
int main()
{
string str;
cout << "Enter a string :" << endl; //Enter String
cin >> str; //Input string
cout << "The number of a's:" << vowelsNumberof (str, 'a') << endl; //Function to count a's
cout << "The number of e's:" << vowelsNumberof (str, 'e') << endl; //Function to count e's
cout << "The number of i's:" << vowelsNumberof (str, 'i') << endl; //Function to count i's
cout << "The number of o's:" << vowelsNumberof (str, 'o') << endl; //Function to count o's
cout << "The number of u's:" << vowelsNumberof (str, 'u') << endl; //Function to count u's
system ("pause");
}
int vowelsNumbersof(string str) //Defines variable string
{
int i, ch = 0; //Defines type and value
for(i=0; i < (int)strlen(); i++) //Sets to 0 and sets length
if(str.at(i)==ch) ch++; //Returns to ith position (0)
return ch;
}
Your function declaration on line 6 and the definition beginning on line 28 do not match.
Line 33 should be for (i = 0; i < str.length(); i++) //Sets to 0 and sets length
I would suggest changing the variable ch's name to count on line 31. It can't be both the count of vowels and the character you're comparing against (line 34).