Thread: Tokenizing a string

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Tokenizing a string

    I am working on a code trying to tokenize a string based on white spaces.I have discovered that when ever i try to copy a char from my untokenised string to my string index array it does not copy.
    Code:
    void parseString(char * unparsed,char **parsed){
        int pos=0;
        while(*unparsed!=NULL){
            while(*unparsed==' '){
                pos++;
                unparsed++;
            }
            (parsed[pos])=parsed[pos] + *(unparsed++);
           
          
        }
      
    }
    
    int main(int argc, char** argv) {
        char *s="44 3 2 + 8 * +\0";
        char *result[12];
        parseString(s,result);
        
        return (EXIT_SUCCESS);
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What exactly do you want result to contain after the execution of parseString?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by oogabooga View Post
    What exactly do you want result to contain after the execution of parseString?
    Code:
    
    index 0=44
    index 1=3
    index 2=2
    index 3=+
    index 4=8
    index 5=*
    index 6 =+
    

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You only update 'pos' if the current character is space. But 'result' is an array of char pointers, you can not assign characters to it directly. You can assign offsets in the original string but each substring will contain all of the remaining string from that point unless you terminate at space with '\0'. This is how functions like strtok and strsep works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tokenizing a string
    By BdON003 in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2009, 10:45 PM
  2. Tokenizing a string
    By chinesepirate in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2008, 11:32 AM
  3. Problem with string tokenizing
    By Mr_Miguel in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 02:02 PM
  4. Tokenizing a C++ string
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2004, 06:31 AM
  5. String Tokenizing
    By irncty99 in forum C++ Programming
    Replies: 21
    Last Post: 05-08-2003, 07:47 AM