pull out first and last words

Discussion in 'C++' started by Bimo Remus, Jun 28, 2003.

  1. Bimo Remus

    Bimo Remus Guest

    Hi, I am currently taking a C++ class and am having problems with a
    homework assignment. My problem is that I need to pull the first and
    last words out of of a character string array which is in the form
    title firstName lastName. The chapter that this assignment is on
    deals with string arrays and includes pointers. I've seen how one can
    reverse the order of letters in a word using pointers so thought maybe
    I could discern the last word from a phrase of an indeterminate number
    of words by going to the last character and keep moving through the
    characters backwards until i reach a ' '. Does anyone have any ideas
    of where I could look?

    Bmo
     
    Bimo Remus, Jun 28, 2003
    #1
    1. Advertising

  2. Bimo Remus

    Aggro Guest

    Bimo Remus wrote:

    > Hi, I am currently taking a C++ class and am having problems with a
    > homework assignment. My problem is that I need to pull the first and
    > last words out of of a character string array which is in the form
    > title firstName lastName. The chapter that this assignment is on
    > deals with string arrays and includes pointers. I've seen how one can
    > reverse the order of letters in a word using pointers so thought maybe
    > I could discern the last word from a phrase of an indeterminate number
    > of words by going to the last character and keep moving through the
    > characters backwards until i reach a ' '. Does anyone have any ideas
    > of where I could look?


    Could you provide some code what you have done so far. Can you read the
    first word from the string?
     
    Aggro, Jun 28, 2003
    #2
    1. Advertising

  3. "Bimo Remus" <> wrote in message
    news:...
    > Hi, I am currently taking a C++ class and am having problems with a
    > homework assignment. My problem is that I need to pull the first and
    > last words out of of a character string array which is in the form
    > title firstName lastName. The chapter that this assignment is on
    > deals with string arrays and includes pointers. I've seen how one can
    > reverse the order of letters in a word using pointers so thought maybe
    > I could discern the last word from a phrase of an indeterminate number
    > of words by going to the last character and keep moving through the
    > characters backwards until i reach a ' '. Does anyone have any ideas
    > of where I could look?
    >
    > Bmo

    Basically you need to read the first word using a for loop, something like
    (assuming you string is in a char array called Array)

    char FirstWord[WhateverNumber];
    for(int i = 0; Array != ' '; i++)
    FirstWord = Array;
    Then read the last word
    char LastWord[WhateverNumber];
    for(int i = (strlen(Array) - 1); Array != ' '; i--)
    LastWord = Array;
    now you have to reverse LastWord (since it is back to front) and you are
    done.
    This would be a lot easier if you used the standard library's string
    container!
    Hope that helps,
    S. Armondi
     
    Samuele Armondi, Jun 29, 2003
    #3
  4. Bimo Remus

    Bimo Remus Guest

    I don't know, but I think we're supposed to do it using pointers and
    srting arrays. I think I understand the concept of the two topics but
    don't really know how to use them correctly. Here're a couple of my
    guesses (keep in mind that the only libraries that I can use are
    <iostream>, <cmath>, <cstdio>, and <cstring>, and maybe another that
    I've forgotten):

    //v.3
    int i = 0;
    for(fullName; fullName != ' '; i++) {
    strcat(title, i);
    }

    //v.4
    char *i, j = 0;
    for(j; j != ' '; j++) {
    i = fullName[j];
    strcat(title, *i);
    }


    yeah, pretty ugly, I know... Here's the compile error:


    Error : function call 'strcat((lval) char *[10], (lval) char)'does not
    match
    'std::strcat(char *, const *)'
    Credit.cpp line 75 strcat(title, i);

    Bimor

    Aggro <> wrote in message news:<lFoLa.411$>...
    > Bimo Remus wrote:
    >
    > > Hi, I am currently taking a C++ class and am having problems with a
    > > homework assignment. My problem is that I need to pull the first and
    > > last words out of of a character string array which is in the form
    > > title firstName lastName. The chapter that this assignment is on
    > > deals with string arrays and includes pointers. I've seen how one can
    > > reverse the order of letters in a word using pointers so thought maybe
    > > I could discern the last word from a phrase of an indeterminate number
    > > of words by going to the last character and keep moving through the
    > > characters backwards until i reach a ' '. Does anyone have any ideas
    > > of where I could look?

    >
    > Could you provide some code what you have done so far. Can you read the
    > first word from the string?
     
    Bimo Remus, Jun 29, 2003
    #4
  5. > Basically you need to read the first word using a for loop, something like
    > (assuming you string is in a char array called Array)
    >
    > char FirstWord[WhateverNumber];
    > for(int i = 0; Array != ' '; i++)
    > FirstWord = Array;
    > Then read the last word
    > char LastWord[WhateverNumber];
    > for(int i = (strlen(Array) - 1); Array != ' '; i--)
    > LastWord = Array;


    Niether string will be null terminated after this code.

    john
     
    John Harrison, Jun 29, 2003
    #5
  6. Bimo Remus

    Bimo Remus Guest

    Thanks, John. It seems so obvious now I'll get back if I'm still having trouble.

    Bimo
     
    Bimo Remus, Jun 29, 2003
    #6
  7. "John Harrison" <> wrote in message
    news:bdlt38$turse$...
    > > Basically you need to read the first word using a for loop, something

    like
    > > (assuming you string is in a char array called Array)
    > >
    > > char FirstWord[WhateverNumber];
    > > for(int i = 0; Array != ' '; i++)
    > > FirstWord = Array;
    > > Then read the last word
    > > char LastWord[WhateverNumber];
    > > for(int i = (strlen(Array) - 1); Array != ' '; i--)
    > > LastWord = Array;

    >
    > Niether string will be null terminated after this code.
    >
    > john
    >
    >

    true, my mistake... it was late though! Thanks for pointing it out
    S. Armondi
     
    Samuele Armondi, Jun 29, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,187
    Mike Treseler
    Jun 28, 2006
  2. Harlin Seritt

    Pull Last 3 Months

    Harlin Seritt, Oct 17, 2007, in forum: Python
    Replies:
    0
    Views:
    247
    Harlin Seritt
    Oct 17, 2007
  3. Harlin Seritt

    Pull Last 3 Months

    Harlin Seritt, Oct 17, 2007, in forum: Python
    Replies:
    11
    Views:
    516
    Hyuga
    Oct 18, 2007
  4. Max Williams
    Replies:
    9
    Views:
    136
    Max Williams
    Jul 20, 2009
  5. Replies:
    2
    Views:
    165
    Tad McClellan
    Mar 26, 2005
Loading...

Share This Page