简体   繁体   中英

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. 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. It accomplishes this by passing a reference to the string object and an index to the character to check; recursion simply adds 1 to this index to check the next character, and so on until the end of the string is found.

I have removed your call to c_str() since it isn't needed. string can be directly indexed.

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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