我需要使用递归来确定给定字符串中某个字符的出现次数。现在非递归函数非常简单。但是,在尝试使用递归时,程序运行时出现错误
short f_r(string, char);
int main()
{
string str;
char c;
cout << "Enter a string: ";
getline(cin,str);
cout << "Enter a character: ";
cin >> c;
cout << "\nString: " << str << endl;
cout << f_r(str,c) << endl;
return 0;
}
short f_r(string str, char c)
{
int pos = 0;
pos = str.find(c, pos);
if (pos > str.length()) return 0;
else
{
int count = 0;
count++;
pos++;
return count + f_r(str,c);
}
}