question about parsing a string??

This is a discussion on question about parsing a string?? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a problem that I'm trying to figure out. I have the following string: something1:something2 but something1 needs ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    15

    question about parsing a string??

    Hi,

    I have a problem that I'm trying to figure out. I have the following string:

    something1:something2

    but something1 needs to be stored into some string variable
    and something2 need to be stored into another seperate string variable. The : is just to seperate the 2 strings, it needs to be ignored.

    I am not at all sure if this can even be done, or if so how to do it??

    I am writing this in C++, but have not figured out how to parse this thing?

    Thanks for any help in advance. It is appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    There are several ways...
    Code:
    char my_string[50] = "word"; //my_string
    char my_string2[50] = "hello"; //my_string2
    cin.getline(my_string, 50);
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    char * szString1 = szInput;
    char * szString2 = strchr(szInput, ':');
    if (szString2 != NULL) {
           *szString2 = '\0'; // Terminate string1 at :
           szString2++;       // Point to start of string2
    }
    Assumes szInput can be modified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM

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