c++ - counting upper case letters in recursive function -
i know wrong, learning how recursive functions , trying understand how work out better.
#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; }
why not returning count number? of 4.
realize each recursive invocation of getupper
has own copy of local variable count
. count++
doesn't useful variable isn't used after increment.
Comments
Post a Comment