# Thread: I have the answer, I'd like to know why.

1. ## I have the answer, I'd like to know why.

Hey Guys,

I'm trying to understand this function 'f'. The code for function f was provided in the book. I wrote the main() function that calls f. The question is "What is the value of f("abcd", "babc")? Here's the code I wrote to try and figure this out:

Code:
```#include <stdio.h>

int f(char *s, char*t);

int main(void){
int dang;
dang = f("abcd", "babc");
printf("dang=%s\n", dang);
return 0;
}

int f(char *s, char*t){
//printf("s=%s, t=%s\n",s,t);
char *p1, *p2;
for(p1=s; *p1; p1++){

for(p2=t; *p2; p2++){
if(*p1==*p2) break;
}
if(*p2== '\0') break;
}
printf("p1=%s, s=%s\n",p1,s); //prints p1=d, s=abcd
return p1-s;
}```
The Answer: The value of f is 3. The reason: The length of the longest prefix of the string s that consists entirely of characters from the string t. Or, equivalently, the position of the first character in s that is not also in t.

How do I print out the answer '3'? In the final printf statement, p1=d, s=abcd. d-abcd = 3? I just don't get it.

In the 2nd question, the value of f("abcd", "bcd") is 0 which I also don't understand. Can someone please help me understand what is happening.

2. The function f() returns an int and you assign the return value to an int (dang), so you also need to print dang as an int in main() (hint: use the %d format specifier).

To answer your second question, what is the first position in the string "abcd" that contains a character that is not also in the string "bcd"? It's the first position, right? That's position 0 (indices are zero-based in C).

By the way, this is the same thing the strspn() function in the standard library does.

3. p1 is a pointer. It's value is the address of 'd', not 'd' itself. Same with s. It is a pointer and it's value is the address of 'a' in the string "abcd". Subtracting s from p1 gives the "distance" between the pointers, which is the answer.

About f("abcd", "bcd"), the answer is 0 since the very first letter of s is not in t, so the scan stops at that point.
Code:
```#include <stdio.h>

int f(const char *s, const char *t) {
const char *p = s;
for (const char *q; *p; ++p) {
for (q = t; *q && *p != *q; ++q) ;
if (!*q) break;
}
return p - s;
}

int main() {
struct {
const char *s, *t;
int n;
} tests[] = {
{"abcd",       "babc", 3},
{"aaaa",       "x",    0},
{"xxxxxxtxxx", "qx",   6},
{"xxxx",       "x",    4},
{"abcd",       "bcd",  0}
};
int size = sizeof tests / sizeof tests[0];

for (int i = 0; i < size; ++i) {
printf("[%s] [%s]\n", tests[i].s, tests[i].t);
int n = f(tests[i].s, tests[i].t);
printf("%d", n);
if (n != tests[i].n)
printf("  WRONG: should be %d", tests[i].n);
putchar('\n');
}

return 0;
}```

4. Hey christop, thank you so much. I can't believe so many of my errors are with silly things like %s vs %d. OMG, so embarrassing. I will look at your reasoning in a bit. I see John.c also made a comment. I'm working on another exercise at the moment and will come back to this.

5. Hey John, thank you for the explanation. I got it now thanks to you and Christop. "It's value is the address of 'd', not 'd' itself. Same with s."

I've read the chapter 3 times and it never covered the content to answer this question. Must have been a prior chapter...

Popular pages Recent additions