I am trying to follow the logic of the following program:
Is the mystery function in effect counting the # of instances a character variable shows up in a string? Please explain the code in the mystery function. I am not getting what is going on when the function calls itself.
here's the whole program. There are three separate character variables being used to call the mystery function.
Code:
#include <stdio.h>
int mystery(char, char * );
main()
{
char ch='i';
char chr = 't';
char charac = 'u';
char str[20]="utilities";
mystery(ch,str);
printf("returned : %d\n",mystery(ch,str));
mystery(chr,str);
printf("returned : %d\n",mystery(chr,str));
mystery(charac,str);
printf("returned : %d\n",mystery(charac,str));
}
int mystery (char ch, char *str)
{
int ans;
if (str[0]=='\0')
ans = 0;
else
if (ch==str[0])
ans=1+mystery(ch, &str[1]); /* what is this doing exactly */
else
ans=mystery(ch,&str[1]); /* what is this doing exactly */
return ans;
}