尝试使用C++中的递归来确定字符串的长度

cor*_*kes 6 c++ string recursion counting

int count(string s){
    if(s == "")
      return 0;
    if(s.length == 1)
      return 1;
    return 1 + count() //This is what I can't figure out. How to traverse the string.
    //I just need a hint, not a full on answer.
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何遍历一个字符串.

NPE*_*NPE 10

提示:用于substr()递归.

此外,您有两个基本案例.其中一个有三个问题:

  1. 它有一个语法错误;
  2. 它依赖于能够计算字符串的长度(这是你的函数应该做的);
  3. 鉴于你有另一个基本案例,这是不必要的.