String Parts

This is a discussion on String Parts within the Windows Programming forums, part of the Platform Specific Boards category; Hey, I have a string like this off a pop server: +OK 6 42638 I need to get the middle ...

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001

    String Parts


    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.

    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    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);


    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
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
                cout << "Listening: ";
                cout << "No Response: ";
            cout << local.port << endl;

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    Something like this will work
    char     *pString=NULL;
    int      iNum=0;
        while(!isdigit(*pString++));//find the first number
        pString--;//back up to the first num
    "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, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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