C++ 如何在递归函数中返回某个布尔值?

C++ 如何在递归函数中返回某个布尔值?,c++,recursion,C++,Recursion,我想制作一个递归函数,确定字符串的字符是否都由字母组成。我就是搞不懂。这是我到目前为止所做的,但它不能正常工作 bool isAlphabetic(string s){ const char *c = s.c_str(); if ((!isalpha(c[0]))||(!isalpha(c[s.size()]))) { return false; } else if (isalpha(c[0])) { isAlphabetic(c+1); return true; } }

我想制作一个递归函数,确定字符串的字符是否都由字母组成。我就是搞不懂。这是我到目前为止所做的,但它不能正常工作

bool isAlphabetic(string s){
const char *c = s.c_str();
if ((!isalpha(c[0]))||(!isalpha(c[s.size()])))
{
    return false;
}
else if (isalpha(c[0]))
{
    isAlphabetic(c+1);
    return true;
}
}

有人能建议一个正确的方法吗?

抛开将要创建的许多部分字符串(考虑只传递字符串和起始索引),检查
isalpha(c[s.size()])
总是会失败,因为这是字符串末尾的
\0
。您还忽略了递归调用的结果

bool isAlphabetic(string s){
  if (s.size() < 1)
    return true;               // empty string contains no non-alphas

  const char *c = s.c_str();

  if (!isalpha(c[0]))
  {
    return false;              // found a non-alpha, we're done.
  }
  else
  {
    return isAlphabetic(c+1);  // good so far, try the rest of the string
  }
}
bool isAlphabetic(字符串s){
如果(s.尺寸()<1)
return true;//空字符串不包含非字母
const char*c=s.c_str();
if(!isalpha(c[0]))
{
return false;//找到一个非alpha,我们完成了。
}
其他的
{
return isAlphabetic(c+1);//很好,到目前为止,请尝试字符串的其余部分
}
}

抛开将要创建的许多部分字符串不谈(考虑只传入字符串和起始索引),检查
isalpha(c[s.size()])
总是会失败,因为这是字符串末尾的
\0
。您还忽略了递归调用的结果

bool isAlphabetic(string s){
  if (s.size() < 1)
    return true;               // empty string contains no non-alphas

  const char *c = s.c_str();

  if (!isalpha(c[0]))
  {
    return false;              // found a non-alpha, we're done.
  }
  else
  {
    return isAlphabetic(c+1);  // good so far, try the rest of the string
  }
}
bool isAlphabetic(字符串s){
如果(s.尺寸()<1)
return true;//空字符串不包含非字母
const char*c=s.c_str();
if(!isalpha(c[0]))
{
return false;//找到一个非alpha,我们完成了。
}
其他的
{
return isAlphabetic(c+1);//很好,到目前为止,请尝试字符串的其余部分
}
}
在此基础上,这里有一个固定的实现,它不会复制字符串的任何部分。它通过传递对
字符串
对象的引用和对要检查的字符的索引来实现这一点;递归只是将1添加到此索引以检查下一个字符,依此类推,直到找到字符串的结尾

我已删除您对
c_str()
的调用,因为它不需要<代码>字符串
可以直接索引

bool isAlphabetic(string const & s, int startIndex = 0) {
    // Terminating case: End of string reached. This means success.
    if (startIndex == s.size()) {
        return true;
    }

    // Failure case: Found a non-alphabetic character.
    if (!isalpha(s[startIndex])) {
        return false;
    }

    // Recursive case: This character is alphabetic, so check the rest of the string.
    return isAlphabetic(s, startIndex + 1);
}
请注意,此函数将空字符串视为字母。您可以通过将
return true
更改为
return!s、 empty()。它通过传递对
字符串
对象的引用和对要检查的字符的索引来实现这一点;递归只是将1添加到此索引以检查下一个字符,依此类推,直到找到字符串的结尾

我已删除您对
c_str()
的调用,因为它不需要<代码>字符串
可以直接索引

bool isAlphabetic(string const & s, int startIndex = 0) {
    // Terminating case: End of string reached. This means success.
    if (startIndex == s.size()) {
        return true;
    }

    // Failure case: Found a non-alphabetic character.
    if (!isalpha(s[startIndex])) {
        return false;
    }

    // Recursive case: This character is alphabetic, so check the rest of the string.
    return isAlphabetic(s, startIndex + 1);
}

请注意,此函数将空字符串视为字母。您可以通过将
return true
更改为
return!s、 empty()

这里是一个工作示例:

#include <iostream>
#include <string>

using namespace std;

bool isAlphabetic(string s)
{
    if( s.empty() )
    {
        return false;
    }

    cout << "checking: " << s[0] << endl;

    if( isalpha(s[0]) )
    {
        return true;
    }

    return isAlphabetic(&s[0]+1);
}

int main()
{
    string word0 = "test";
    if( isAlphabetic(word0) )
    {
        cout << word0 << " is alphabetic" << endl; 
    }
    else
    {
        cout << word0 << " is NOT alphabetic" << endl; 
    }

    string word1 = "1234";
    if( isAlphabetic(word1) )
    {
        cout << word1 << " is alphabetic" << endl; 
    }
    else
    {
        cout << word1 << " is NOT alphabetic" << endl; 
    }

    string word2 = "1234w";
    if( isAlphabetic(word2) )
    {
        cout << word2 << " is alphabetic" << endl; 
    }
    else
    {
        cout << word2 << " is NOT alphabetic" << endl; 
    }

   return 0;
}
#包括
#包括
使用名称空间std;
布尔isAlphabetic(字符串s)
{
如果(s.empty())
{
返回false;
}

cout这里有一个工作示例:

#include <iostream>
#include <string>

using namespace std;

bool isAlphabetic(string s)
{
    if( s.empty() )
    {
        return false;
    }

    cout << "checking: " << s[0] << endl;

    if( isalpha(s[0]) )
    {
        return true;
    }

    return isAlphabetic(&s[0]+1);
}

int main()
{
    string word0 = "test";
    if( isAlphabetic(word0) )
    {
        cout << word0 << " is alphabetic" << endl; 
    }
    else
    {
        cout << word0 << " is NOT alphabetic" << endl; 
    }

    string word1 = "1234";
    if( isAlphabetic(word1) )
    {
        cout << word1 << " is alphabetic" << endl; 
    }
    else
    {
        cout << word1 << " is NOT alphabetic" << endl; 
    }

    string word2 = "1234w";
    if( isAlphabetic(word2) )
    {
        cout << word2 << " is alphabetic" << endl; 
    }
    else
    {
        cout << word2 << " is NOT alphabetic" << endl; 
    }

   return 0;
}
#包括
#包括
使用名称空间std;
布尔isAlphabetic(字符串s)
{
如果(s.empty())
{
返回false;
}

cout“但是它不能正常工作”它怎么不工作?错误的结果?崩溃?它在每个测试用例中都返回false为什么你首先需要递归?一个简单的
for()
循环不适合解决这个问题吗?
!isalpha(c[s.size())
毫无意义;为什么要测试空终止符是否是字母数字字符?(提示:它从来不是字母数字字符。)此外,还需要
返回isAlphabetic(c+1);
而不是调用它然后返回true;您只是忽略了递归调用的结果。@Joey12您是否已使用调试器查看发生了什么?(提示请参阅cdhowie的注释)“但它工作不正常”它是如何工作的?错误的结果?崩溃?它在每个测试用例中都返回false为什么您首先需要递归?一个简单的
for()
循环不适合解决这个问题吗?
!isalpha(c[s.size()])
没有意义;为什么您要测试null终止符是否是字母数字字符?(提示:它从来不是字母数字字符。)此外,您需要
返回isAlphabetic(c+1);
而不是调用它然后返回true;您只是忽略了递归调用的结果。@Joey12您是否使用了调试器来查看发生了什么?(提示请参阅cdhowie的注释)如果你从空字符串开始怎么办?我想这会破坏这个。它需要额外的逻辑步骤来检查空字符串。刚刚捕获到。谢谢。如果你从空字符串开始怎么办?我想这会破坏这个。它需要额外的逻辑步骤来检查空字符串。刚刚捕获到。谢谢。你可以为空返回false通过将
return true
更改为
return!s.empty()
…返回true可能更好,isAlphabetic的含义是“对于s中的所有c,c是alpha”,对于空集也是如此。@JimBalter事实上,如果这是所需的行为,那么这是一个不错的选择。将此添加到我的答案中。您可以通过将
return true
更改为
return!s.empty()
…来为空字符串返回false。尽管返回true可能更好,因为isAlphabetic的意思是“对于s中的所有c,c都是alpha”,这对于空集是正确的。@JimBalter的确,如果这是所需的行为,那么这是一个不错的选择。将此添加到我的答案中。