problem with string delimeter

This is a discussion on problem with string delimeter within the C Programming forums, part of the General Programming Boards category; Hi, i wanna know how to tokenize the string out of delimeter('--').For example: aaa bbb-ccc -- eee fff ggg The ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    problem with string delimeter

    Hi, i wanna know how to tokenize the string out of delimeter('--').For example:

    aaa bbb-ccc -- eee fff ggg

    The question i want to ask is, i want to token the word aaa bbb-ccc out, but
    i am having trouble it give me (aaa bbb) not (aaa bbb-ccc), i know the problem is because
    when it met '--' even is '-' it also token it out..Can i have any tips on this? Thanks.

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Post the code you've written so far so we can help you further.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Sorry bout that..
    Here is part of my code
    Code:
     while (fgets(read_line, 100, in) != NULL)
      { 
      
       for (i=0; i<strlen(read_line); i++)
        {
        /* i think my logic here is wrong */
         if ((read_line[i] == '-') && (read_line[i+1] == '-'))
         {
             ptr = strtok(read_line, "--"); 
         }
        }
    
        ptr = strtok(read_line, "\n");
      
        while(ptr != NULL)
        {
          
          strcpy(line, ptr);
          fprintf(out, "%s\n", line);
       
        ptr = strtok(NULL, "\n");
    }
       
      }
    For Example:
    Read in input are:
    abc abc-abb -- bbb aaa eee

    Output should be:
    abc abc-abb

    but my output is:
    abc abc

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    There is a much cleaner, nicer way of doing this but I never pass up a chance to do some good ol' fashioned useless string hacking

    Code:
    int main(int argc, char *argv[])
    {
        char str[] = "abc abc-bbb -- bbb aaa eee";
        char* dst = new char[20];
        char* p = &str[0];
        char* d = &dst[0];
    
        memset(dst, 0, 20);
        while (*p != '-') *d++ = *p++;
        while (*p != 32) *d++ = *p++;
        while (*p != 'b') p++;
        *d = 0;
    
        std::cout << "tokens:\n" << p << "\n" << dst << std::endl;
    
        return 0;
    }
    Output:

    Code:
    tokens:
    bbb aaa eee
    abc abc-bbb
    Press any key to continue
    EDIT: Damn this the C board. Can we just ignore the 'std::cout' - I can't be bothered to change it
    I also realise my method is too problem-specific and relies on the user entering that exact string of characters.
    Last edited by cboard_member; 05-03-2006 at 06:33 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Ok..i will try it out..thanks

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Another way:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       const char text[] = "abc abc-abb -- bbb aaa eee";
       char desired[32], *found = strstr(text, " --");
       if ( found )
       {
          sprintf(desired, "%.*s", found - text, text);
          printf("desired = \"%s\"\n", desired);
       }
       return 0;
    }
    
    /* my output
    desired = "abc abc-abb"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

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