#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    9
    Rep Power
    0

    Question How To Split a string into words


    Hi, my program is suppose to read in a line of string (in C, Not C++) and separate them into words and save it for different field (last name, first name... etc). but before i proceed to saving steps, i have problem separating them correctly, for example:
    (basic UNIX)>>a.out
    (my input) >>hi, Me A. 2154
    it suppose to return:
    hi
    Me
    A
    2154

    however, it doesn't work and instead, it would return me with:
    hi
    A
    A
    A
    (which doesn't work so well when it checks for '(spaces)')
    What is wrong? o_O? ( i tried to use strtok, but it won't even scan after the first space; and also this code looks kinda long -- this is only for the first part of separating the actual link of string would be contain more space in between: hi, Me A. 2415 teach 1982) THANK YOU so MUCH for helping me!

    here is my code:
    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    int i;
    char message[50], *current, *last, *find;
    gets(message);
    last=message;

    while((current=strchr(last, ','))!=NULL)
    { *current++='\0';
    find=last;
    last=current;
    } /* while: scan ID number */
    puts(find);

    while((current=strchr(last, ' '))!=NULL)
    { *current++='\0';
    find=last;
    last=current;
    } /* while: scan title */
    puts(find);

    while((current=strchr(last, '.'))!=NULL)
    { *current++='\0';
    find=last;
    last=current;
    } /* while: scan salary */
    puts(find);

    while((current=strchr(last, ' '))!=NULL)
    { *current++='\0';
    find=last;
    last=current;
    } /* while: check space */

    while((current=strchr(last, '\n'))!=NULL)
    { *current++='\0';
    find=last;
    last=current;
    } /* while: scan salary */
    puts(find);
    return;
    }
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: How To Split a string into words


    I would use strtok, like you attempted to do before. You should be aware of exactly how it works, though. From MSDN:

    On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    9
    Rep Power
    0

    Lightbulb it works


    wow, i try it with your instruction and it works this time!
    i program as the following to test, and hopefully this is what you mean....
    my program code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    char message[50], *current, *last;

    gets(message);
    last=message;

    current=strtok(last, ",");
    puts(current);

    current=strtok(NULL, " ");
    puts(current);

    current=strtok(NULL, ".");
    puts(current);

    current=strtok(NULL, " ");
    puts(current);

    current=strtok(NULL, " ");
    puts(current);

    current=strtok(NULL, " ");
    puts(current);

    return;}

    >> May i just ask one more question, this is reading a line of string, n it works fine; if i am reading a file that contains lines of data, should i put a "while" loop at the beginning to test it until it reach "EOF". last time i try, it gave me a infinite loop... so... thanks again *(^_^)*
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Great. However, it would be best to place the consecutive calls with NULL in a loop, so it can handle a string with any number of words.

    strtok
    Return Value
    Returns a pointer to the next token found in strToken. They return NULL when no more tokens are found.
    Last edited by Jason Doucette; April 18th, 2003 at 12:03 PM.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    9
    Rep Power
    0
    wow, you respond so fast.... anywaz, i added the loop and it works fine EXCEPT... when it returns me the output the very last part is:
    (input)>>Me, you S. 241 work 817.05
    (output)>>Me
    you
    S
    241
    work
    817.05



    Segmentation fault
    >>
    -- it reads from a file of data, n that file contains several lines of strings. the very last string ends with a new line (nothing is after the new line), i tried to use "scanf" to test the EOF, problem become worse; it won't read the data and still ends with Seg.Fault..... (-_-#) Thank you again

    code:
    int main(int argc, char *argv[])
    {
    int n, index=1;
    char message[100], *current, *last;
    FILE *fpin, *fpout;

    if((fpin=fopen(argv[index], "r"))==NULL)
    { printf("Data file couldn't be opened.\n");
    return;
    } /* if: test for successful file opening */

    while(!feof(fpin))
    { fgets(message, 100, fpin);
    last=message;
    current=strtok(last, ",");
    puts(current);
    current=strtok(NULL, " ");
    puts(current);
    current=strtok(NULL, ".");
    puts(current);
    current=strtok(NULL, " ");
    puts(current);
    current=strtok(NULL, " ");
    puts(current);
    current=strtok(NULL, " ");
    puts(current);
    } /* while: scan until end of file */

    return;}
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    you're overstepping the bounds and trying to access space that isnt there. try using a loop like this:


    while(tokenPtr != NULL)
    {
    tokenPtr = strtok(NULL, " ");
    }

    this way, u always check to see if there is more to tokenize before u do the operation.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    9
    Rep Power
    0
    but which part I should replace this with? if i replace "while(fpin!=NULL)" with "while(current!=NULL)" and then... it still return me with the same result... at the end it's still Seg.Fault.....
    just one more question... how come it trys to access space and return me "seg.Fault" statement, but when it perform the test to the previous strings, it doesn't happen?
    what is Segmentation fault actually representing?
    Thank you~ you r helping me with linked list too~ Thanx *(^_^)*
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by mulderGURL
    but which part I should replace this with?
    As I read what Infamous is telling you, you will replace from the first puts to the last one, inclusive. Though I would add one puts statement in the while loop:
    Code:
    while(current != NULL)
    {
        puts(current);
        current = strtok(NULL, " ");
    }

    Originally posted by mulderGURL
    what is Segmentation fault actually representing?
    That you are trying to access memory outside of the chunk of memory allotted you. This is most commonly due to trying to use a bad pointer value, such as NULL (that would hit the interrupt vector table, which can be a very bad thing for the entire system).

    The reason why it was happening here is because strtok returns a NULL when there are no more tokens in the input string.

IMN logo majestic logo threadwatch logo seochat tools logo