strpbrk()

Discussion in 'C Programming' started by amanayin, Aug 11, 2003.

  1. amanayin

    amanayin Guest

    size_t strcspn(char *str1, char *str2);
    char *strpbrk(char *str, char *accept);

    In the book i am using it says:

    The function strpbrk() returns a pointer to the first
    character in str that matches any of the characters in accept.
    If it doesn't find a match, the function retuns NULL. As
    previously explained for the function strchr(), you can
    obtain the offset of the first match in str by subtracting
    the pointer str from the pointer returned by strpbrk()
    (if it isn't NULL, of cource).
    For example, replace strcspn() on line 21 with strpbrk();

    But when i do i get the following error
    strcspn.c:21: warnig: assignment makes integer
    from pointer without a cast

    does this error have something to do with the functions
    or have i missinterprtated what is said above could
    some one please explain. And how do you use strpbrk()
    properly

    /* STRCSPN.C DEMONSTRATES THE STRSCPN() FUNCTION */

    #include<stdio.h>
    #include<string.h>

    int main(void)
    {
    char buf1[80], buf2[80];
    size_t loc;

    /* INPUT THE STRINGS. */

    printf("Enter the string to be searched: ");
    fgets(buf1,80,stdin);
    printf("Enter the string containing target characters: ");
    fgets(buf2,80,stdin);
    buf2[strlen(buf2)-1] = 0;

    /* PERFORM THE SEARCH */

    loc = strcspn(buf1, buf2);

    if(loc == strlen(buf1))
    printf("No match found\n");
    else
    printf("The first match was found at position %d.\n",loc);
    return 0;
    }
     
    amanayin, Aug 11, 2003
    #1
    1. Advertising

  2. amanayin

    Dan Pop Guest

    In <bh7ola$ima$> amanayin <> writes:

    >size_t strcspn(char *str1, char *str2);
    >char *strpbrk(char *str, char *accept);
    >
    >In the book i am using it says:
    >
    >The function strpbrk() returns a pointer to the first
    >character in str that matches any of the characters in accept.
    >If it doesn't find a match, the function retuns NULL. As
    >previously explained for the function strchr(), you can
    >obtain the offset of the first match in str by subtracting
    >the pointer str from the pointer returned by strpbrk()
    >(if it isn't NULL, of cource).
    >For example, replace strcspn() on line 21 with strpbrk();
    >
    >But when i do i get the following error
    >strcspn.c:21: warnig: assignment makes integer
    >from pointer without a cast
    >
    >does this error have something to do with the functions
    >or have i missinterprtated what is said above could
    >some one please explain. And how do you use strpbrk()
    >properly


    What is the return type of strcspn? What is the return type of strpbrk?
    You can't make an arbitrary change to a C program and still expect it to
    be a correct C program.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 11, 2003
    #2
    1. Advertising

  3. amanayin

    amanayin Guest

    At this point in my learnig i lack the understanding so it
    would be appreciated if you could take the program and
    correct it for me so i have a example to go on this would
    enable me to see what your taking about.

    Thanks for your help.
     
    amanayin, Aug 12, 2003
    #3
  4. amanayin

    Dan Pop Guest

    In <bha8q9$2hl$> amanayin <> writes:

    >At this point in my learnig i lack the understanding so it
    >would be appreciated if you could take the program and
    >correct it for me so i have a example to go on this would
    >enable me to see what your taking about.


    At any time in your learning process you should not play with programs
    you don't fully understand!

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 12, 2003
    #4
  5. amanayin

    Ben Pfaff Guest

    (Dan Pop) writes:

    > At any time in your learning process you should not play with programs
    > you don't fully understand!


    Why not? That's the way to learn. If I hadn't spent lots and
    lots of time playing with programs I didn't fully understand, I
    wouldn't be a competent programmer today.
     
    Ben Pfaff, Aug 12, 2003
    #5
  6. amanayin

    Dan Pop Guest

    In <> Ben Pfaff <> writes:

    > (Dan Pop) writes:
    >
    >> At any time in your learning process you should not play with programs
    >> you don't fully understand!

    >
    >Why not? That's the way to learn.


    It's the WRONG way to learn. If you don't understand the program, how can
    you tell whether its output is correct by design or by accident? What can
    you learn from such a program? What can you learn from the changes you
    make to such a program? C is not the kind of language one can learn by
    blindly experimenting.

    The correct way to learn is to try to understand the program first.
    Then, by playing with it, you check whether your understanding was
    correct or not.

    >If I hadn't spent lots and
    >lots of time playing with programs I didn't fully understand, I
    >wouldn't be a competent programmer today.


    Or maybe you'd be a lot more competent today if you tried to understand
    the programs you played with.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 13, 2003
    #6
  7. amanayin

    amanayin Guest

    /* STRPBRK.C DEMONSTRATES THE STRPDRK() FUNCTION */
    /* COME BACK TO THIS ONE */

    #include<stdio.h>
    #include<string.h>

    int main(void)
    {
    char buf1[100], buf2[100];
    char *p, *c;
    int loc;

    /* INPUT THE STRINGS. */

    printf("Enter the string to be searched: ");
    fgets(buf1+1,100,stdin);
    buf1[strlen(buf1)-1] = 0;

    printf("Enter the string containing target characters: ");
    fgets(buf2,100,stdin);
    buf2[strlen(buf2)-1] = 0;

    /* ASSIGN POINTERS */

    p = buf1;
    c = buf2;

    /* PERFORM THE SEARCH */

    loc = strlen(p = strpbrk(p+1,c));

    if(loc == 0)
    printf("Not found\n");
    else
    printf("%u\n",loc);

    return 0;
    }

    What have i done wrong when no match is found it gives the ouput
    segmentation fault. If this code wants flaming, flame away but
    put it in terms a begginer can understand nothing criptic please
     
    amanayin, Aug 13, 2003
    #7
  8. amanayin

    amanayin Guest

    Owe i for got to say it counts back wards
    example if i input first string "how now"
    and i input second string "how"
    out put reads "7"
     
    amanayin, Aug 13, 2003
    #8
  9. amanayin

    Ben Pfaff Guest

    (Dan Pop) writes:

    > In <> Ben Pfaff <> writes:
    >
    > > (Dan Pop) writes:
    > >
    > >> At any time in your learning process you should not play with programs
    > >> you don't fully understand!

    > >
    > >Why not? That's the way to learn.

    >
    > It's the WRONG way to learn. If you don't understand the program, how can
    > you tell whether its output is correct by design or by accident? What can
    > you learn from such a program? What can you learn from the changes you
    > make to such a program? C is not the kind of language one can learn by
    > blindly experimenting.
    >
    > The correct way to learn is to try to understand the program first.
    > Then, by playing with it, you check whether your understanding was
    > correct or not.


    In other words, you should play with programs you don't fully
    understand. "Try to understand" != "fully understand".
    --
    "I don't have C&V for that handy, but I've got Dan Pop."
    --E. Gibbons
     
    Ben Pfaff, Aug 13, 2003
    #9
  10. amanayin

    Ben Pfaff Guest

    (Dan Pop) writes:

    > In <> Ben Pfaff <> writes:
    >
    > > (Dan Pop) writes:
    > >
    > >> In <> Ben Pfaff <> writes:
    > >>
    > >> > (Dan Pop) writes:
    > >> >
    > >> >> At any time in your learning process you should not play with programs
    > >> >> you don't fully understand!
    > >> >
    > >> >Why not? That's the way to learn.
    > >>
    > >> It's the WRONG way to learn. If you don't understand the program, how can
    > >> you tell whether its output is correct by design or by accident? What can
    > >> you learn from such a program? What can you learn from the changes you
    > >> make to such a program? C is not the kind of language one can learn by
    > >> blindly experimenting.
    > >>
    > >> The correct way to learn is to try to understand the program first.
    > >> Then, by playing with it, you check whether your understanding was
    > >> correct or not.

    > >
    > >In other words, you should play with programs you don't fully
    > >understand. "Try to understand" != "fully understand".

    >
    > You either understand something or you don't.


    If understanding is binary, black or white, why did you use the
    phrase "fully understand"?

    > Your understanding may not be correct, so you need to validate
    > it by playing with the program. Trying to understand the
    > program by playing with it is the hopelessly wrong aproach.


    *shrug* Often works for me. If you think you need to understand
    a program fully before trying to modify it, you haven't ever
    needed to make a small modification to a large program.
    --
    "I don't have C&V for that handy, but I've got Dan Pop."
    --E. Gibbons
     
    Ben Pfaff, Aug 14, 2003
    #10
  11. amanayin

    CBFalconer Guest

    Ben Pfaff wrote:
    > (Dan Pop) writes:
    >

    .... snip ...
    >
    > > Your understanding may not be correct, so you need to validate
    > > it by playing with the program. Trying to understand the
    > > program by playing with it is the hopelessly wrong aproach.

    >
    > *shrug* Often works for me. If you think you need to understand
    > a program fully before trying to modify it, you haven't ever
    > needed to make a small modification to a large program.


    When 'perturbing' an existing program, the most important thing to
    know is the scope of the perturbation. With luck the original
    programmer has marked the routine static, and it communicates
    solely through its parameters. In practice, however ....

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 15, 2003
    #11
  12. amanayin

    Dan Pop Guest

    In <> Ben Pfaff <> writes:

    > (Dan Pop) writes:
    >
    >> In <> Ben Pfaff <> writes:
    >>
    >> > (Dan Pop) writes:
    >> >
    >> >> In <> Ben Pfaff <> writes:
    >> >>
    >> >> > (Dan Pop) writes:
    >> >> >
    >> >> >> At any time in your learning process you should not play with programs

    ^^^^^^^^^^^^^^^^^^^^^^^^
    >> >> >> you don't fully understand!
    >> >> >
    >> >> >Why not? That's the way to learn.
    >> >>
    >> >> It's the WRONG way to learn. If you don't understand the program, how can

    ^^^^^^^^^^^^^^^^^^
    >> >> you tell whether its output is correct by design or by accident? What can
    >> >> you learn from such a program? What can you learn from the changes you
    >> >> make to such a program? C is not the kind of language one can learn by
    >> >> blindly experimenting.
    >> >>
    >> >> The correct way to learn is to try to understand the program first.

    ^^^^^^^^
    >> >> Then, by playing with it, you check whether your understanding was
    >> >> correct or not.
    >> >
    >> >In other words, you should play with programs you don't fully
    >> >understand. "Try to understand" != "fully understand".

    >>
    >> You either understand something or you don't.

    >
    >If understanding is binary, black or white, why did you use the
    >phrase "fully understand"?


    To emphasise "understand". Many people consider they understand
    something when they have a very approximative idea about it.

    >> Your understanding may not be correct, so you need to validate
    >> it by playing with the program. Trying to understand the
    >> program by playing with it is the hopelessly wrong aproach.

    >
    >*shrug* Often works for me. If you think you need to understand
    >a program fully before trying to modify it, you haven't ever
    >needed to make a small modification to a large program.


    I neither think so nor expressed this opinion. I was talking about
    modifying programs for LEARNING purposes. Once you have learned C,
    it is perfectly possible to use the hacking approach to program
    maintenance. This is how I actually maintain all the software I
    haven't written myself in the first place. The piece of code you
    need to understand for this purpose is typically larger than a whole
    program used by beginners who are in the process of learning the
    language.

    Try to keep your comments focused to the context of the discussion.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 17, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dreamcatcher

    what strspn and strpbrk stands for ?

    dreamcatcher, Sep 19, 2003, in forum: C Programming
    Replies:
    9
    Views:
    1,291
    Dan Pop
    Sep 23, 2003
Loading...

Share This Page