pull out first and last words

B

Bimo Remus

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
 
A

Aggro

Bimo said:
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?
 
S

Samuele Armondi

Bimo Remus said:
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
 
B

Bimo Remus

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
 
J

John Harrison

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
 
B

Bimo Remus

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

Bimo
 
S

Samuele Armondi

John Harrison said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top