Thread: String Parts

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    String Parts

    Hey,

    I have a string like this off a pop server:

    +OK 6 42638

    I need to get the middle number (6) in an int varible, so i think i just need to split the string up and use atoi(), can someone tell me how to do this baring in mind that the 6 could be any number.

    Thanks
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Take the array and cycle through it, ignoring whitespace untill a char is found (use a flag ). This means you have reached the 'O' in '"OK". After this, ignore whiespace till the next char is found and start copying, turning copying off when a space is encountered. Cast the copied text with atoi(). The drawbacks of this is that the functionality is limited to that program. A better solution is to write a functin with this prototype (which is reusable):

    int get_next_number(char str[], int *next_position);

    whereas:

    int next = 0;

    char string[] = "Get 2 numbers from this 1 string";

    int two = get_next_number(string, &next);
    int one = get_next_number(string, &next);

    "two" should now contain the value '2', and "one" the value '1'.

    Hope that helps
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Something like this will work
    Code:
    char     *pString=NULL;
    int      iNum=0;
    
    
    pString=sTextBuffer;
    while(*pString!='\0')
    {
        while(!isdigit(*pString++));//find the first number
        pString--;//back up to the first num
        iNum=atoi(pString);
        break;
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM