C++ string functions

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)

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

using namespace 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;
}
Last edited on
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).

But I get the declaration error where

"declaration of 'int ch' shadows a parameter"

I just want to read each character, and update the corresponding vowel given the character is a lowercase vowel.

I also did what you said about changing the name of the variable ch to count, but then I just got:

[Linker error] undefined reference to `vowelsNumberof(std::string, char)'

a bunch of times...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string::size_type vowelsNumbersof( const std::string &s, char c )
{
   const char vowels[] = "aeiou";

   std::string::size_type count = 0;

   if ( std::strchr( vowels, c ) != 0 )
   {
      for ( char c2 : s )
      {
         if ( c2 == c ) count++; // or count += c2 == c; instead of the if statement
      }
   }

   return count;
}
 

Last edited on
Sorry, but I can't use any type of array...
Last edited on
And what is the problem? Substitute it for an object of type std::string or remove this part of the function.
Oh... Lemme try that...
Topic archived. No new replies allowed.