473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 19 '05 #1
6 10825
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?

Jul 19 '05 #2
"Bimo Remus" <bm******@yahoo.com> wrote in message
news:3f**************************@posting.google.c om...
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

Jul 19 '05 #3
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 <sp**********@yahoo.com> wrote in message news:<lF***************@read3.inet.fi>...
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?

Jul 19 '05 #4
> 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
Jul 19 '05 #5
Thanks, John. It seems so obvious now I'll get back if I'm still having trouble.

Bimo
Jul 19 '05 #6
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bd************@ID-196037.news.dfncis.de...
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
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Lau | last post by:
What is the best way to compare two entries in a single table where the two fields are "almost" the same? For example, I would like to write a query that would compare the first two words in a...
0
by: bdtmike | last post by:
I'm using the GridView control and have the Mode of the Pager set to "NumericFirstLast". However, when there are several pages to display, the pager shows only page numbers--and no First/Last...
2
by: Tony Ciconte | last post by:
Does anyone know of or have any VBA code or similar logic that can help distinguish similar first/last name combinations? For example, we would like to prompt the user of a possible match when any...
3
by: mahir78 | last post by:
I want to get first 10 words in a column in sql query. (by using space) any help is appreciated
8
by: shapper | last post by:
Hello, I have a string which holds a text. Is it possible to create a substring which uses the first N words of that string? Thanks, Miguel
2
by: Janick Bernet | last post by:
I just stumbled upon this olap function and wondered why the "NULLS FIRST/LAST" clause is not possible in normal order by but only using row_number() over(). Sure, I can do a SELECT *,...
5
tolkienarda
by: tolkienarda | last post by:
hi all i need to select the first fifteen words of an article stored in a mysql database. so i have the basic idea laid out but the exact syntax seems a bit confusing. i have a book here on...
1
by: saurcurban | last post by:
Hi, Following is the xml file: ------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <wc> Delhi is the capital of India
13
by: Sharkiness | last post by:
Morning All, I cannot find the answer to my question anywhere. I have an address field named 'ADDRESS'. I want to run an update query so that the first two words of the address are split into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.