473,809 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10887
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.goo gle.com...
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(ch ar *, const *)'
Credit.cpp line 75 strcat(title, i);

Bimor

Aggro <sp**********@y ahoo.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.dfn cis.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
1881
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 "company" field. If they are the same, I would like to output them. For example, "20th Century" and "20th Century Fox" in the company field would be the same.
0
1759
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 buttons. First of all, is this a bug or am I doing something stupid? Secondly, can I override the page numbers using my own template? I'm using the latest Visual Studio 2005 Thanks.
2
2529
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 of the first/last names Robert Smith, or Bob Smith, or Robt. Smith are used during data entry. All that is necessary is to alert the user so that they can review any appropriate existing records. We have done several google searches without any...
3
6869
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
4997
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
9897
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 *, row_number() over(ORDER BY col1 ASC NULLS FIRST) AS rn FROM tab1 ORDER BY rn to get what I want, but its awfully complicated. (Interestingly, the optimizer rewrites above order by in an ugly ORDER BY CASE WHEN col1 is NULL THEN 0 ELSE 1, col1) Anyone...
5
1500
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 regular expressions and i think it has confused more than before. so here is basicaly what i want to do select the first word what ever it may be, so /S+ i think should match 1 or more non white space charters and then a /s then repete that fifteen...
1
2515
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
6865
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 a new field named 'AddressSplit'. Therefore if the full address showed the following 'Ruby Cottage Edinburgh Scotland'
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.