Slitting a string

This is a discussion on Slitting a string within the C++ Programming forums, part of the General Programming Boards category; Hi, i am having some trouble finding help on how to split a string here is an example of the ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Slitting a string

    Hi, i am having some trouble finding help on how to split a string here is an example of the string:

    2us234us2us123us34us209u

    I want this to be changed to a 2d array with

    us | 234
    us | 2
    us | 123
    us | 34
    us | 209

    I can make things easier by making the number always 3 digits, but this takes up room on the robots program space. One thing i can do is put dilimiters in like "." between 'chunks' (a chunk is two letters follwed by a number) and a "," between data so it becomes:

    2.us,234.us,2.us,123.us,34.us,209.u

    The reason that there are bits of the chunks at the begging and end of the string is that the string is comming form a device which is always sending the data even if no cable is connected or a program is running.

    So, anyone know how i could achieve this?

    Thanks In Advance,

    Martyn

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    So you want to separate letters and numbers? Use isalpha() and isdigit()
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    Hi,

    I must ask, is there a website with a list of C++ commands commonly used where i can find examples and the command structure of the commands you suggested?

    Would be a lot faster and easier for me to find what i need that way.

    Thanks,

    Martyn

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Ive made this. If you assume that your string always begin with "us" this code should work fine. If I made any mistakes, pleas tell me.
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main(){
    
       string myString = "us234us234us2us123us34us209";
       istringstream myInput(myString);
       
       const int MAX_SIZE = 60;
       
       /* initalize the array */
       int array[MAX_SIZE];
       int position = 0;
          
       while(myInput){
          /* you can ignore the characters or put them elsewere */
          myInput.ignore(2);
          /* save the number */
          myInput >> array[position++];
       }
       
       return 0;
    }
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    a more general version, uncompiled:
    Code:
    string fullString;
    string letterString;
    string digitString;
    typedef vector<string> Instruction;
    vector<Instruction> instructions;
    Instruction instruction;
    bool numerical = false;
    
    //ignore leading digitString
    int i;
    while(isdigit(fullString[i]) )
    {
      ++i;
    }
    //current char must be a letter, given restrictions of problem.
    letterString += fullString[i++];
    //then parse original string 1 char at a time until null char found
    while(fullString[i] != '\0')
    {
      //if current char is a letter
      if(isalpha(fullString[i]))
      {
        //if this is first letter of a new letter string
        if(numerical)
        {
          //add current digitString to current Instruction
          instruction.push_back(digitString);
          //current Instruction complete so add vector of Instructions
          instructions.push_back(instruction);
          //no clear current Instruction for next instruction
          instruction.pop_back();
          instruction.pop_back();
          //clear current digitistring for next digitString
          digitString = "";
        }
        //add current char to current letterString
        letterString+= fullString[i++];
        //flip flag
        numerical = false;
      }
      /else do the converse
      else if(isdigit(fullString[i]))
      {
        if(!numerical)
        {
           instruction.push_back(letterString);
           letterString = "";
        }
        digitString += fullString[i++];
        numerical = true;
      }
    }

  6. #6
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    http://www.cppreference.com
    not completely, but has lots of interesting stuff and is a very good reference. i have all the STL pages (vectors, deques, strings) saved to my HD, sitting on my desktop, so i can get them whenever i want, as well as half of the rest.

    Apart from that......
    http://www.google.com
    Ph33r the sphericalCUBE

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    Thanks for your help all!

    Google is good, but too good.

    Problem is i can inadvertadantly start reading somming in C or PERL etc. which is a waste of time!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 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, 07:49 PM

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