Splitting

This is a discussion on Splitting within the C++ Programming forums, part of the General Programming Boards category; I've read the tutorials, read other peoples source code, and they all do more than I'm looking for which helps ...

  1. #1
    Unregistered
    Guest

    Splitting

    I've read the tutorials, read other peoples source code, and they all do more than I'm looking for which helps to my confusion, all i'm basically looking for is to split a string sentence, for example:

    "An Example" would be split into 2 words, "An" and then the other "Example", most of you guys would be able to do this in seconds, but with the more I read on this, the more my head spins. If anyone would like to write an example to only split a sentence for me, I would be very grateful.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions all depending on what data structure you use including array, string, and cstring. In the following solution, I use STL string.

    -----
    #include <vector>
    #include <string>
    using std::string;

    std::vector<string> myWords;
    string mySentence("A sentense");
    string::size_type begPos = 0,
    endPos = 0;
    endPos = mySentence.find(' ', begPos);

    while (endPos != string::npos)
    {
    myWords.push_back(mySentence.substr(begPos, endPos);
    begPos = ++endPos;
    endPos = mySentence.find(' ', begPos);
    }
    -----

    Note the algorithm above may have minor bugs. Look it over and modify it to according to your needs.

    Kuphryn

  3. #3
    Unregistered
    Guest
    if you don't want to use stl classes you could use strtok() using space as the delimiter or devise your own function.

    Code:
    char phrase[] = "An Example";
    int length = strlen(phrase);
    int i, j;
    char temp[100];
    j = 0;
    for(i = 0; i < length; i++)
    {
       if(phrase[i] != ' ')
         temp[j++] = phrase[i];
       else
       {
          temp[j] = '\0';
          //now do something with temp
          cout << temp << endl;
          j = 0;
        }
    }

  4. #4
    Unregistered
    Guest
    oh yeah, since there is no space at the end of Example you need to add the following lines at the end of the for loop if you use my example:

    temp[j] = '\0';
    //do something with temp here, too.
    cout << temp << endl;

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Code:
    char *split_sentence( char *fStr, char *bStr )
    {
       while( isspace( *fStr ) ) fStr++;
       while( *fStr != '\0' ) {
         if( *fStr == ' ' ) {
            fStr++;
            break;
         }
         *bStr++ = *fStr++;
       }
       *bStr = '\0';
       while( isspace( *fStr ) ) fStr++;
       return fStr;
    }
    To Use:

    Code:
    char Command[128];
    
    Buffer = split_sentence( Buffer, Command );
    if u need to split CStrings, one other way not mentioned yet would be to use the c_str() function.

    Code:
          char * Buffer = new char[ txtLastCommand->Text.Length() + 1 ];
          strcpy( Buffer, txtLastCommand->Text.c_str() );
          Buffer = split_sentence( Buffer, Command );
    Hope it helps.
    Scorpion-ice
    Imperium et Respectus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. winsock splitting msg
    By abraham2119 in forum Networking/Device Communication
    Replies: 10
    Last Post: 07-05-2009, 04:24 AM
  2. String splitting algorithm help
    By (TNT) in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2007, 11:28 AM
  3. Problem splitting program into different files
    By kzar in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:03 AM
  4. Splitting the Window
    By cweb255 in forum Windows Programming
    Replies: 4
    Last Post: 05-30-2005, 06:19 AM
  5. Splitting a dialog up into multiple classes
    By Just in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 11:11 PM

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