我知道這是錯誤的,但只是學習如何做遞歸函數並試圖理解如何更好地工作。在遞歸函數中計算大寫字母
#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