Recursive return of a character

I am writing to display the number of a's in a user input. I am getting an out of range at memory location. I am not sure what I am doing wrong with my code, any suggestion would be 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
#include <iostream>
#include <string>

using namespace std;

//Creating constants
int countA(string, char);
string userInput;
const char character = 'a';

int main()
{
	//Explain program to user
	cout << "This program will take your input, and calculate the number of a's entered" << endl;

	//Prompt user to enter string
	cout << "Please enter a short sentence: ";
	cin >> userInput;
	
	//Call the recursive part
	cout << "The count of a's in the sentence is: " << countA(userInput, character) << endl;
	system("PAUSE");
	return 0;
}

int countA(string str, char character)
{
	//Initialize the count
	int count = 0;

	//Read the input into an array
	if (userInput [0] == character)
	{
		//Returning count
		return count;
	}
	else
	{
		//Returning count
		count++;
		return count + countA(str.substr(1), character);
	}
}
Last edited on
You read only one word, not "sentence".

You don't check, whether you are at the end of the string. Hence the crash.

Use of global variables is unnecessary. Reuse of name "character" for both global variable and function parameter is confusing.

If the word starts with 'a', then there is no recursion at all and the result is 0, no matter how many a's the word has.
Okay I appreciate your input, I have made some changes to the global variables. But I am not sure how to get it to check for the end of the string. Here are my changes thus far.

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

using namespace std;

//Creating constants
int countA(string, char);

int main()
{
	string userInput;
	const char character = 'a';

	//Explain program to user
	cout << "This program will take your input, and calculate the number of a's entered" << endl;

	//Prompt user to enter string
	cout << "Please enter a short sentence: ";
	cin >> userInput;
	
	//Call the recursive part
	cout << "The count of a's in the sentence is: " << countA(userInput, character) << endl;
	system("PAUSE");
	return 0;
}

int countA(string str, char character)
{
	string userInput;

	//Initialize the count
	int count = 0;

	//Read the input into an array
	if (userInput [0] == character)
	{
		//Returning count
		return count;
	}
	else
	{
		//Returning count
		count++;
		return count + countA(str.substr(1), character);
	}
}
How long is a piece of std::string? When is a std::string empty?

Note, that you create an empty string on line 29, but compare its first(?) element on line 35. What does that help? The user-given input is not there.
That is a good point, thanks for your input. I have removed the information that you have indicated. I am trying to wrap my head around what data needs to be in my if statement there.
So for instance, if I try something like this it just gives me back the count of letters in the input.

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

using namespace std;

//Creating constants
int countA(string, char);

int main()
{
	string userInput;
	const char character = 'a';

	//Explain program to user
	cout << "This program will take your input, and calculate the number of a's entered" << endl;

	//Prompt user to enter string
	cout << "Please enter a short sentence: ";
	cin >> userInput;
	
	//Call the recursive part
	cout << "The count of a's in the sentence is: " << countA(userInput, character) << endl;
	system("PAUSE");
	return 0;
}

int countA(string str, char character)
{
	//Initialize the count
	int count = 0;

	//Read the input into an array
	if (str == "")
	{
		//Returning count
		return count;
	}
	else
	{
		//Returning count
		count++;
		return count + countA(str.substr(1), character);
	}
}
1
2
3
4
5
6
7
8
9
10
size_t countA( string str, char token )
{
  if ( str.empty() ) {
    return 0;
  }
  else {
    const size_t count = // 0 or 1, depending on whether token==str[0]
    return ...
  }
}


Recursion on problem like this is obviously extremely inefficient.
I don't understand what you are saying, I am new to c++ so all of that doesn't make sense to me without an explanation. Whether or not this is "extremely inefficient" it's how I have been asked to write it. I need to write a program that returns the number of "a's" in a phrase entered by a user. I appreciate the help keskiverto, but is there anyone else that is willing to help me out with this one?
I have changed this thing around a little and am getting correct results but only for the first word, it will not do it for the entire string. Whats a good way to do that?

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

using namespace std;

string:: size_type vowelsNumbersof(const string &string, char character)
{
	const char vowels[] = "aeiou";

	string::size_type count = 0;

	if (std::strchr(vowels, character) != 0)
	{
		for (char character2 : string)
		{
			if (character2 == character) count++; 
		}
	}
	return count;
}

int main()
{
	string str;

	//Explain program to user
	cout << "This program will take your input, and calculate the number of a's entered" << endl;

	//Prompt user to enter string
	cout << "Please enter a short sentence: ";
	cin >> str;
	
	//Call the recursive part
	cout << "The number of a's:" << vowelsNumbersof(str, 'a') << endl; //Function to count a's

	cout << "The number of e's:" << vowelsNumbersof(str, 'e') << endl; //Function to count e's

	cout << "The number of i's:" << vowelsNumbersof(str, 'i') << endl; //Function to count i's

	cout << "The number of o's:" << vowelsNumbersof(str, 'o') << endl; //Function to count o's

	cout << "The number of u's:" << vowelsNumbersof(str, 'u') << endl; //Function to count u's

	system("PAUSE");
	return 0;
}
cin >> str will read one word. If you want the entire line, do getline(cin, str);

Note that your vowelsNumbersof() function is not recursive. Does it need to be? The comment suggests that it should.
Last edited on
Thank you! I totally forgot about the getline function.
Topic archived. No new replies allowed.