Recursion:Recursion: Recursion:Recursion

Recursion:Recursion: Recursion:Recursion

Post by Dave Towns » Sat, 04 Dec 2004 14:59:30



I'm
pointer

truly
Your code has a bug in it which your professor must have missed, shame on
him!.
If you pass a string where the first character is the max character, your
function
will miss it!

Try this - here I've saved the first character of the string before
incrementing s.

char maxChar(char *s)
{
char c;
char max;
if(*s == '\0')
return *s;
max = *s;
c = maxChar(++s);
if(c>max)
return c;
else
return max;
}

The reasoning behind this algorithm is as follows. Take the first character
of
the string and pass the remaining characters to maxChar recursively.
Compare
the results, choose the max and return that. If a string is empty, then 0
is the max
char value.