 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bimo Remus Guest
|
Posted: Sat Jun 28, 2003 8:35 pm Post subject: pull out first and last words |
|
|
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
|
|
| Back to top |
|
 |
Aggro Guest
|
Posted: Sat Jun 28, 2003 10:29 pm Post subject: Re: pull out first and last words |
|
|
Bimo Remus wrote:
| Quote: | 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?
|
|
| Back to top |
|
 |
Samuele Armondi Guest
|
Posted: Sat Jun 28, 2003 11:44 pm Post subject: Re: pull out first and last words |
|
|
"Bimo Remus" <bmoremus (AT) yahoo (DOT) com> wrote
| Quote: | 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] != ' '; i++)
FirstWord[i] = Array[i];
Then read the last word
char LastWord[WhateverNumber];
for(int i = (strlen(Array) - 1); Array[i] != ' '; i--)
LastWord[i] = Array[i];
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
|
|
| Back to top |
|
 |
Bimo Remus Guest
|
Posted: Sun Jun 29, 2003 1:52 am Post subject: Re: pull out first and last words |
|
|
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] != ' '; 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 <spammerdream (AT) yahoo (DOT) com> wrote
| Quote: | 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?
|
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sun Jun 29, 2003 5:22 am Post subject: Re: pull out first and last words |
|
|
| Quote: | 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] != ' '; i++)
FirstWord[i] = Array[i];
Then read the last word
char LastWord[WhateverNumber];
for(int i = (strlen(Array) - 1); Array[i] != ' '; i--)
LastWord[i] = Array[i];
|
Niether string will be null terminated after this code.
john
|
|
| Back to top |
|
 |
Bimo Remus Guest
|
Posted: Sun Jun 29, 2003 1:11 pm Post subject: Re: pull out first and last words |
|
|
Thanks, John. It seems so obvious now I'll get back if I'm still having trouble.
Bimo
|
|
| Back to top |
|
 |
Samuele Armondi Guest
|
Posted: Sun Jun 29, 2003 2:58 pm Post subject: Re: pull out first and last words |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: | 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] != ' '; i++)
FirstWord[i] = Array[i];
Then read the last word
char LastWord[WhateverNumber];
for(int i = (strlen(Array) - 1); Array[i] != ' '; i--)
LastWord[i] = Array[i];
Niether string will be null terminated after this code.
john
true, my mistake... it was late though! Thanks for pointing it out |
S. Armondi
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|