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

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    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>
    // http://knking.com/books/c2/answers/c13.html
    
    
    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. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    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. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    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;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    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. #5
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    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 subscribe to a feed

Similar Threads

  1. Preferred answer length? Answer reuse value?
    By Nominal Animal in forum General Discussions
    Replies: 13
    Last Post: 10-29-2015, 01:19 PM
  2. Can anyone answer please
    By sid2387 in forum C Programming
    Replies: 1
    Last Post: 05-03-2015, 05:23 AM
  3. Replies: 9
    Last Post: 08-13-2012, 03:19 AM
  4. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 10:12 PM
  5. have 5 min to answer
    By juststartedC in forum C Programming
    Replies: 7
    Last Post: 10-11-2007, 05:26 PM

Tags for this Thread