Thread: HELLLLP!!!! string parsing C++ only

  1. #1
    waki
    Guest

    HELLLLP!!!! string parsing C++ only

    Please change my code to see if name 1 is = to name2 listed in an array of strings- should i make the following code a function. I am thinking of making the following code a function(get token) and then use if for each name. Then I would use a compare function to compare each name. How am i passing the array through the parameters and how would the compare function go...

    here is my code so far...
    #include < iostream >
    #include < string >
    using namespace std;

    int main()
    {
    int len1; int pos_sp, pos_comma, pos_period;
    // position of space, etc
    string name1, partial_name; string token1, token2, token3 = " ";
    name1 = "trixie, cee c."; // string w/4 tokens
    len1 = name1.length(); // get length of string
    pos_sp = name1.find(' '); // get location of 1st space
    token1 = name1.substr(0,pos_sp); // get 1st token
    partial_name = name1.substr(pos_sp + 1, len1 - 1); // create new string
    //print 1st token and remainder of string
    cout << endl << token1 << endl << partial_name << endl;
    //now scan for a period pos_period = token1.find('.'); if (pos_period < 0)
    cout << "No period found" << endl;
    else
    {
    token1 = token1.substr(0, pos_period); // remove the period
    cout << "I have removed the period" << endl;
    cout << token1 << " this token was an initial" << endl;
    }
    //now scan for a comma pos_comma = token1.find(','); if (pos_comma < 0)
    cout << "No period found" << endl;
    else
    {
    token1 = token1.substr(0, pos_comma); // remove the comma
    cout << "I have removed the comma " << endl;
    cout << token1 << " this token was a LAST name" << endl;
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I suggest you review the methods in the string class in your text/on line help. Using the string class removes a lot of the hassle of using the underlying c_style string, but there is still a reasonable amount of programming skill necessary use it. Learning by experience is an excellent technique and figuring out how to use your references early on will make your experience a whole lot more pleasurable.

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. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM