Thread: Spliting strings?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    69

    Spliting strings?

    I was making a console type program, but the only part im stuck on is spliting the commands that the user inputs. For example. Lets say the user inputs--"exec C:/blah.exe". I wan't to split "exec", and "C:/blah.exe" apart. Some function kinda like this.

    split(buffer, string, delimiter);

    Also, is it possible to make the buffer an array of somesort, so it puts the first part in buffer[0] which would be "exec", and buffer[1] would contain "C:/blah.exe";

    I don't know of any such functions, but if you could point me in the right direction then I would greatly appreciate it!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Try one of the online C/C++ references.

    In C I would use strchr() or strtok(), but unfortunately I don't know the C++ equivalent.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I'm used to using Split in VB applications. I found this code where the join and split were ported over.

    http://www.codetoad.com/c_join_split.asp

    I use the code in a c+ program and it's great.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by dwks
    Try one of the online C/C++ references.

    In C I would use strchr() or strtok(), but unfortunately I don't know the C++ equivalent.
    I have a feeling im gonna be making my first gigantic function.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why? Try strtok().

    strtok().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or, better yet, try here and look at find or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Looking into strtok. Thanks! Ill tell you how it works out.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Ok, well the only problem I have is since im using strings in my prog I use c_str(); to convert them into const char*, but this strok function want's char* instead of const char*. Any ideas?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Copy the const char* into a buffer, and use the buffer as an argument to strtok().

    Or use the C++ function[s] (which I don't know about yet).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by dwks
    Copy the const char* into a buffer, and use the buffer as an argument to strtok().

    Or use the C++ function[s] (which I don't know about yet).
    Hmm, well I wonder if there are any alternatives to c_str().
    I don't like the fact that it assumes that your never gonna change the value of that string.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use either a stringstream or a combination of find and substr. The stringstream works just like cin and can parse your string easily (use getline with your delimiter if you have only one). The substr method of the C++ string class can get a substring from the original like Left, Mid, or Right.

    You don't want to use the C string methods with C++ strings because they weren't designed for that. Use the C++ methods, or make a C-style copy of the string if you really want to use strtok.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    This looks right to me. Note that charInput is an std::string. I also didn't post all of the code since it is quite large, and these are the only 2 functions you really need to see.
    Code:
    void CTextFunc::getInput()
    {
    	ba("");
    	std::getline(cin, charInput);
    	if(boolInputLimit==true){
    		if(strlen(charInput.c_str())<=usintCharInputLimit){
    		exec(charInput.c_str());
    		getInput();
    		} else {
    		ban("Error! Your command is to long!");
    		ba("The maximum amount of character's allowed is \'");
    		sprintf(charIntBuffer, "%i", usintCharInputLimit);
    		b(charIntBuffer);
    		bn("\'");
    		getInput();
    		}
    	} else {
    		exec(charInput.c_str());
    	}
    }
    
    void CTextFunc::exec(const char* charString)
    {
    	strtok (charString," ");
    	getInput();
    }
    Of course the exec function doesn't work since charString is is a const char*, and I can't figure out how to convert it into a char*. Could you show me an example with using strtok then?
    Last edited by bladerunner627; 09-27-2005 at 04:29 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char newbuffer[strlen(charString)+1];
    strcpy(newbuffer, charString);
    Or use C++ functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A stringstream would be ideal here - and much simpler than your current code. There are a lot of existing string functions that you aren't taking advantage of. For example, there is no need to use strlen with a C++ string. It has a length() method.

    BTW, using dwks code above to copy a C++ string into a C style string needs to be strcpy(newbuffer, charString.c_str());

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    using dwks code above to copy a C++ string into a C style string needs to be
    Code:
    strcpy(newbuffer, charString.c_str());
    Code:
    void CTextFunc::exec(const char* charString)
    It does?

    [edit]dwks' code:
    Code:
    strcpy(newbuffer, charString);
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. spliting strings
    By tyroiusrtmaonm in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2007, 12:21 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM