-3
bool wm(const char *s, const char *t)
{
    return *t-'*' ? *s ? (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1) : !*t : wm(s,t+1) || *s && wm(s+1,t);
}

I have search the internet for ternary/if else equivalents, but this one seems weird because it has a return in the beginning.

from cplusplus website: (condition) ? (if_true) : (if_false)

if(a > b){
    largest = a;
} else if(b > a){
    largest = b;
} else /* a == b */{
    std::cout << "Uh oh, they're the same!\n";
}

Thank You

  • 1
    Your two code blocks seem entirely unrelated to each other. What are you trying to accomplish? – DiMono Mar 01 '13 at 23:43

3 Answers3

0

It is two ternary statements actually.

if (*t-'*' ) {
  if (*s) {
    return (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1);
  } else {
    return !*t;
  }
} else {
  return wm(s,t+1) || *s && wm(s+1,t);
}
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

The return in the beginning just returns the result of the entire statement.

In your case, you could write this as:

bool wm(const char *s, const char *t)
{
    if(*t-'*')
    {
        if (*s)
           return (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1);
        else
           return !*t;
    }
    else
        return wm(s,t+1) || *s && wm(s+1,t);
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

The return is not part of the ternary expression. You can think of it like:

return (
  *t-'*' ? *s ? (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1) : !*t : wm(s,t+1) || *s && wm(s+1,t)
);

To duplicate this as an if statement, you'll need to place a return statement in the separate branches:

if (*t-'*') {
  if (*s) {
    return (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1);
  } else {
    return !*t;
  }
} else {
  return wm(s,t+1) || *s && wm(s+1,t);
}
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324