T
Thrillhouse
I'm a college computer science major and I'm studying for my final exam by
going over previous tests. There's one question that I answered correctly
but I cannot, for the life of me, figure out how it works. The machine I'm
currently on does not have any environment or compiler so I can't test it.
The question was to write a recursive function that's passed a char pointer
and returns the maximum valued character in the string. Here's what I
wrote:
char maxChar(char *s)
{
char c;
if(*s == '\0')
return *s;
c = maxChar(++s);
if(c>*s)
return c;
else
return *s;
}
Can somebody explain to me how this works? How can maxChar return the
largest valued character to the first call of it if it can never reach the
if-else until the end of the string? I know it sounds stupid but I'm truly
dumbfounded by it. Thanks for the help.
going over previous tests. There's one question that I answered correctly
but I cannot, for the life of me, figure out how it works. The machine I'm
currently on does not have any environment or compiler so I can't test it.
The question was to write a recursive function that's passed a char pointer
and returns the maximum valued character in the string. Here's what I
wrote:
char maxChar(char *s)
{
char c;
if(*s == '\0')
return *s;
c = maxChar(++s);
if(c>*s)
return c;
else
return *s;
}
Can somebody explain to me how this works? How can maxChar return the
largest valued character to the first call of it if it can never reach the
if-else until the end of the string? I know it sounds stupid but I'm truly
dumbfounded by it. Thanks for the help.