Thread: Slitting a string

  1. #1
    Registered User
    Join Date
    Apr 2003

    Slitting a string

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


    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:,,,,,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,


  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Seattle, WA
    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."

  3. #3
    Registered User
    Join Date
    Apr 2003

    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.



  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    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.
    #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;
          /* you can ignore the characters or put them elsewere */
          /* save the number */
          myInput >> array[position++];
       return 0;
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Registered User
    Join Date
    Mar 2002
    a more general version, uncompiled:
    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]) )
    //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 this is first letter of a new letter string
          //add current digitString to current Instruction
          //current Instruction complete so add vector of Instructions
          //no clear current Instruction for next instruction
          //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]))
           letterString = "";
        digitString += fullString[i++];
        numerical = true;

  6. #6
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    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......
    Ph33r the sphericalCUBE

  7. #7
    Registered User
    Join Date
    Apr 2003
    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, 02: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, 06:49 PM