Re: extract substring

Discussion in 'C Programming' started by goose, Aug 13, 2003.

  1. goose

    goose Guest

    (Andrew) wrote in message news:<>...
    > Does anyone know of a simple way to extract substrings from a string
    > based on delimiters such as comma's? I am trying the following to
    > print each word seperated with a comma within the string, but this
    > function does not work. Am I overlooking a simpler approach to this.
    > Thanks
    >
    > char* teststring = "Hello,There,World,Anyone";
    > char* testlook = ",";
    > char* testtmp;
    >
    > while(strstr(teststring,testlook)!=NULL) {
    >
    > testtmp = (char*)malloc(sizeof(char)*(strlen(teststring)-
    > strlen(strstr(teststring,testlook))+1));
    >
    > strncpy(testtmp,teststring,(strlen(teststring)-
    > strlen(strstr(teststring,testlook))));
    >
    > strcat(testtmp,"\0");
    >
    > printf("The word is:%s",testtmp);
    >

    at this point you really *should* free the memory pointed to
    by testtmp[1], or else on the next run of the loop, you are going
    to lose this memory.

    free (testtmp);
    >
    > strcpy(teststring,strstr(teststring,testlook));


    this is UB[2] I do believe. because the source and destination
    strings overlap, you are not allowed to copy from source to
    destination (or vice-versa). lookup the function "memmove" in
    your C book.

    >
    > }


    1. You also never check to make sure that malloc was able
    to allocate you a big enough block of memory.

    2. Undefined Behaviour. this means that the code is allowed
    to do anything at all, including crashing, working or making
    the milk in your fridge curdle. writing past the end of an array
    is UB, and writing into a string literal is UB, and using
    strcpy on strings that overlap is UB.

    hth
    goose,
    UB is even allowed to make your hubbie go fishing, or
    cause your wife to get pregnant :)
    goose, Aug 13, 2003
    #1
    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. Petterson Mikael

    how to extract substring

    Petterson Mikael, Apr 6, 2006, in forum: Java
    Replies:
    7
    Views:
    19,585
    Petterson Mikael
    Apr 7, 2006
  2. Ed Morton

    Re: extract substring

    Ed Morton, Aug 8, 2003, in forum: C Programming
    Replies:
    2
    Views:
    451
    Kevin Easton
    Aug 9, 2003
  3. Tim Hagan

    Re: extract substring

    Tim Hagan, Aug 8, 2003, in forum: C Programming
    Replies:
    3
    Views:
    533
    Dave Thompson
    Aug 25, 2003
  4. Paul Hsieh

    Re: extract substring

    Paul Hsieh, Aug 25, 2003, in forum: C Programming
    Replies:
    20
    Views:
    1,018
    Joona I Palaste
    Sep 4, 2003
  5. Replies:
    3
    Views:
    191
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page