Thread: String Help

  1. #1
    Registered User blackwyvern's Avatar
    Join Date
    Jan 2002
    Posts
    60

    Question String Help

    My question is this... I want to be able to enter a string into my program, like "The dog jumped." Then I want the program to be able to separate the 3 separate words... How can I do this?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: String Help

    Originally posted by blackwyvern
    My question is this... I want to be able to enter a string into my program, like "The dog jumped." Then I want the program to be able to separate the 3 separate words... How can I do this?
    The string library has some pretty handy stuff for doing just that. Include <string> in your source code. Look up the function strtok( ). Basically it takes 2 parameters. The first is a pointer to a character string that you want to "tokenize". For example, your first argument might be "Hello World". The next argument is the delimeter. This is probably going to be " " just a whitespace string. The function will return to you the first token it finds as a pointer to a character string. If it does not find a valid token, it will return null. When you call the function again to get the next token (i.e. world in our example) you pass NULL as the first parameter. If you need some source or something just let me know.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    kuphryn
    Guest
    You could just make up an algorithm yourself.

    Code:
    #include <vector>
    #include <string>
    std::string szText = "Testing 1 2 3";
    std::vector<std::string> szWordList;
    int indexA = 0,
         indexB = 0;
    
    while (indexB != -1)
    {
       indexB = szText.find(' ', indexA);
       
       if (indexB != -1)
      {
          szWordList.push_back(szText.substr(indexA, indexB - indexA
          ++indexB;
          indexA = indexB;
       }
    }
    Kuphryn

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Simple source code to your problem. Not very ellegant but I whipped it up pretty quick.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    elad
    Guest
    I you don't have the STL available in your compiler, then the string and vector classes as well as the find() algorhythm won't be available to you.

    strtok() works on null terminated strings (too).

    you can write your own function looking for space char without using the find algorhythm. Just evaluate each char in the input buffer to see if it is a space. If it isn't transfer the char to a holding buffer. if it is transfer a NULL char to the holding buffer. Transfer the data in the holding buffer somewhere else and go on to the next char in the input buffer.

  6. #6
    Nick
    Guest
    I wouldn't recommend using strtok because it's almost
    as complicated has doing it by hand and is kind of strange
    since it uses a static variable.

    By hand you can do something
    like

    Code:
    void skip_ws(ifstream& in)
    {
          int c;
    
          while((c = in.get()) && isspace(c))
                continue;
    
          in.putback(c);
    }
    Or if you like c you can use fgetc and ungetc. Since most
    of the manipulation is going to be so low level
    this is what I would do.

    Once your done this in a loop you can call skip_ws.
    Because you have skipped all the white space you know that the next charecter read in is going to
    be part of a token but you could have a situation like
    c = foo+5; where your program would tokenize
    it as
    'c' '=' 'foo+5;' so you will have to add more code to tokenize
    it like 'c' '=' 'foo' '+' '5'
    While you are tokenizing it you will want to decide that
    'c' is a indentifier '=' is an operator 'foo' is an indentifier and
    '5' is a numerical constant.

  7. #7
    Nick
    Guest
    I guess you won't have to worry about compiling
    the dog jumped! Maybe I've been studying too long.

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Use the cin.getline function to read an entire line.

    char* buffer;
    cin.getline(buffer, 40, '\n');

    Reads up to 40 chars and stops at the newline character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  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. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM