1. ## recursive function

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;
}```

2. ## Re: recursive function

mystery counts the number of times the character ch occurs in the string str

the logic is
if length of the string is 0 return 0 as there cannot be any occurence of ch
if the first character of the string is ch , no of times the character ch occurs in the string is 1+(no of times the character ch occurs in str[1], .str[2],........)
else the number of occurences of ch is just the number of times the character ch occurs in str[1],str[2],................

Originally posted by Peachy
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;
}```

3. PINKO:

how is the string being access if there is no incrementation of
str ?? how is str[2] , str[3] being checked against ch??? I don't see where there is incrementation to next element of string each time mystery function is called. Maybe there is a better way to ask this, but I am confused how each character of the string is being accessed here.

Code:
```
else
if (ch==str[0])
ans=1+mystery(ch, &str[1]);```

And in the else (if ch is not = to str[0])
this part of code is not adding 1 to the 'count' of ans variable, right???

Code:
```else
ans=mystery(ch,&str[1]);```

4. you can think of &str[1] as a pointer to the string starting at str+1
the code could have been equivalently written as
else
if (ch==str[0])
ans=1+mystery(ch,str+1);

so the function mystery is being applied str+1
from where if necessary it may be applied to (str+1)+1 etc....

to see that &str[1] and str+1 correspond to the same address run this code

#include <stdio.h>

int main(void)
{
char str[100],*p,*q;
p=str+1;
q=&str[1];
printf("p:%p q:%p",p,q);
return 0;
}

Originally posted by Peachy
PINKO:

how is the string being access if there is no incrementation of
str ?? how is str[2] , str[3] being checked against ch??? I don't see where there is incrementation to next element of string each time mystery function is called. Maybe there is a better way to ask this, but I am confused how each character of the string is being accessed here.

Code:
```
else
if (ch==str[0])
ans=1+mystery(ch, &str[1]);```

And in the else (if ch is not = to str[0])
this part of code is not adding 1 to the 'count' of ans variable, right???

Code:
```else
ans=mystery(ch,&str[1]);```

5. ok so, say str[10] = "balance";

&str[1] will look at this as " alance "
not as " a " (the first 'a' in the string) ??

and str+1 will do the same as &str[1] ???

6. Originally posted by Peachy
ok so, say str[10] = "balance";

&str[1] will look at this as " alance "
not as " a " (the first 'a' in the string) ??

and str+1 will do the same as &str[1] ???
Yes , in this case .

To test this simply run this program

#include <stdio.h>

int main(void)
{
char str[]="balance";
printf("%s\n%s",str+1,&str[1]);
return 0;
}