something like strtok

This is a discussion on something like strtok within the C Programming forums, part of the General Programming Boards category; The problem: I have a string in a pointer or in an array.I want to seperate the words from this ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    63

    something like strtok

    The problem:
    I have a string in a pointer or in an array.I want to seperate the words from this string and drop them into a list.Example: we have string "my name is Bond". I want to seperate the words like: "my" and drop it to the list in the first node, "name" and drop it in the second node, "is" and "Bond" the same.
    Forget the list, I know how to make this
    My problem is that i must write a fynction to seperate the words in a string but i don't know how to begin.Any idea? thanks!


    GO to post 18

    something like strtok
    Last edited by alzar; 01-18-2008 at 12:16 PM.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    What are the characters that separate the words? How would you define a set of instructions to separate the words from each other, that should apply to all sentences?

    Wouldn't be something like..

    1. Find the first space.
    2. Read characters from 0 to first space.
    3. Find next space.
    4. Read characters from (position of first space + 1) to next space.
    5. Rinse and repeat 3 and 4 until finished.

    Now program.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what, exactly, is wrong with using strtok() for this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    @matsp
    i must write my function

    @IceDane
    Yes i thought something like this but i think that:
    every time i find a space i must replace it with \0 or not? Because i must pass this word as a string into a list.
    Another question: my thought is to use an array of pointers to have the words and then drop its word into the list.Am i correct?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, ok. Are you allowed to use ANY str* functions? Would for example "strchr" or "strspn" be valid to use?

    And yes, you have to replace the space with a zero character. [Actually, you don't have to, if you use some other method to remember how long the word was, but I don't see why you would want to use that method - it just complicates matters].

    Yes, using an array of pointers, as long as it's long enough - worst case is half the number of characters in the string [e.g. "a a a a a a a a a a"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    I can use only strlen and strcpy from str functions
    So you say that is possible to have an array like that : "hello/0my/0name/0is/0"
    But how i pass the string to the array from strings( char *str[5]) with strcpy? should i remember everytime where is the first letter from each word example h m n and i ?

    which method did you mean that is more simple? this with "strchr" or "strspn"?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you still have just one char *, not an array of char *.

    If you create a new char *[], then each entry will point to a new string, e.g.
    Code:
        char *strings[SOMENUMBER];
        
        strings[0] = "my";
        strings[1] = "name";
        strings[2] = "is";
    strcpy() needs to be passed one of those strings at a time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Code:
        int len;
        char t[10]="hello wor";
        int i, j , last;
    
        char *words[3];
        
        i=0;
        len=strlen(t);
       while(t[i]<len){
            if(t[i]= ' '){
              last=i;
              strcpy(words[0], t);}
              i++;
              }
         
       printf(words[0]);
    don't seems to work? I can't understand what will be the second argument of strcpy

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That code definitely doesn't work since you haven't allocated any memory for "words[0]" - you only have the memory for the words array itself, but there is no memory for the strings in there.

    Also, you don't set the space to zero, so the string is still "Hello Wo" when you do the copy.

    In the final code, you will also need to have a pointer to where in t you started the search for the second word.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    so i need malloc to allocate memory for *words[0] ?
    Maybe the idea of array *words[3] is not so good .Is there a way to do this without the use of malloc

    some changes
    Code:
        int len,l;
        char t[10]="hello wor";
        int i, j , last;
        char words[7];
        
        i=0;
        len=strlen(t);
       while(t[i]<len){
            if(t[i]= ' '){
              last=i;
              t[i]='\0';
              strcpy(words, t);}
              i++;
              t[0]=t[i];/*set the t to point to "wor"*/
              }

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could set "words[x]" to the beginning of the current word, perhaps.

    And you can't move the entire string with
    Code:
    t[0] = t[i]
    , that just sets the first char to the one at t[i] (presumably 'w' - so now t is "wello wo" instead).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Code:
        i=0;
        len=strlen(t);
       while(i<len){
            if(t[i]= ' '){
              last=i;
              t[i]='\0';}
              printf(t);
              i++;
              }
    Ok first this part of code.I think that this code should print only hello but it prints hello wor

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm surprised it prints anything visible at all, actually - you are overwriting everything with space, because you are using a single equal sign when you should be comparing to space using two equal signs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    i don't understand. You mean that i should use if(t[i]== ' ') or something else?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by alzar View Post
    i don't understand. You mean that i should use if(t[i]== ' ') or something else?
    Yes, that would prevent "t[i]" from being overwritten with a space in the if-statement.

    This doesn't explain why the printout is as it is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21