alazar gebeyehu
alazar gebeyehu

Reputation: 33

Can I use the conditional operator like this?

!isalpha( str[first] )  ? ( return isPalindrome( str, ++first, last ) ) : return isPalindrome( str, first, --last ) ;

I get a syntax error.

Upvotes: 1

Views: 74

Answers (2)

ruakh
ruakh

Reputation: 183251

That's not allowed, because return is not allowed inside an expression; it's only allowed at the top-level of a statement. (Any expression can be used as a statement, but the reverse is not true.) You can write either this:

return !isalpha(str[first])
           ? isPalindrome(str, ++first, last)
           : isPalindrome(str, first, --last);

or this:

if (!isalpha( str[first] )) {
    return isPalindrome( str, ++first, last );
} else {
    return isPalindrome( str, first, --last );
}

Upvotes: 6

greatwolf
greatwolf

Reputation: 20838

return !isalpha(str[first]) ? 
       isPalindrome(str, ++first, last) : 
       isPalindrome(str, first, --last);

Upvotes: 4

Related Questions