我知道这是错误的,但只是学习如何做递归函数并试图理解如何更好地工作。在递归函数中计算大写字母
#include <iostream>
using namespace std;
int getUpper (const string& s, int high) {
int count=0;
if(s.size()>0) {
if (s[0] <='Z' && s[0] >='A')
count++;
return getUpper(s.substr(1,s.size()-1), high-1);
}
return count;
}
int getUpper (const string& s){
int high=s.size()-1;
int count=getUpper(s,high);
return count;
}
int main()
{
string s="WeLC";
int value=getUpper(s);
cout << value;
return 0;
}
为什么这不返回计数数? 4.
作为一个方面说明,'std :: count_if'是这样做的正确方法。 – chris