简体   繁体   English

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

[英]How to return a certain boolean value in a recursive function?

I want to make a recursive function that determines if a string's characters all consist of alphabets or not. 我想做一个递归函数,确定一个字符串的字符是否全部由字母组成。 I just can't figure it out. 我只是想不通。 Here's what I've done so far but it doesn't work properly. 这是我到目前为止所做的,但无法正常工作。

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;
}
}

can anyone suggest a correct way? 谁能提出正确的方法?

Leaving aside the many partial strings you'll create (consider passing in just the string and a starting index instead), the isalpha(c[s.size()]) check will always fail, since that's the \\0 at the end of the string. 撇开您将要创建的许多部分字符串(考虑只传递字符串和起始索引), isalpha(c[s.size()])检查将始终失败,因为这是\\0结尾字符串。 You're also ignoring the result of the recursive calls. 您还忽略了递归调用的结果。

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
  }
}

Building on Paul's answer , here is a fixed implementation that won't copy any portion of the string. Paul的答案为基础 ,这是一个固定的实现,不会复制字符串的任何部分。 It accomplishes this by passing a reference to the string object and an index to the character to check; 它通过传递对string对象的引用和对要检查的字符的索引来实现此目的; recursion simply adds 1 to this index to check the next character, and so on until the end of the string is found. 递归只是向该索引加1以检查下一个字符,依此类推,直到找到字符串的结尾。

I have removed your call to c_str() since it isn't needed. 我已经删除了您对c_str()调用,因为它不是必需的。 string can be directly indexed. string可以直接索引。

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);
}

Note that the empty string is considered alphabetic by this function. 请注意,此功能将空字符串视为字母。 You can change this by changing return true to return !s.empty() . 您可以通过将return true更改为return !s.empty()来进行更改。

Here a working example: 这是一个工作示例:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM