[C++] String Tokenizer with Delimiter {Novice Programmer}

A

AMT2K5

Hello, how would I go about breaking up a string that is returned by a
function. After I do that, I will strcpy that data to a class data
member [which I know how].

I have the following functions

void Account::getPhoneNumber(char st[]) //Retrieves the phone number
{
}

void Account::getLastName(char st[]) //Retrieves the last name
{
}

void Account::getFirstName(char st[]) //Retrieves the first name
{
}

void Account::getCity(char st[]) //Retrieves the city name
{
}

and the following function returns the string, getCustomer();
which has the string: "James,Harry,Toronto,416-555-5555;"

So the delimiter is a comma with the final data having a semicolon at
the end.

Appreciate any help, thanks in advance.
 
H

Howard

AMT2K5 said:
Hello, how would I go about breaking up a string that is returned by a
function. After I do that, I will strcpy that data to a class data
member [which I know how].

I have the following functions

void Account::getPhoneNumber(char st[]) //Retrieves the phone number
{
}

void Account::getLastName(char st[]) //Retrieves the last name
{
}

void Account::getFirstName(char st[]) //Retrieves the first name
{
}

void Account::getCity(char st[]) //Retrieves the city name
{
}

and the following function returns the string, getCustomer();
which has the string: "James,Harry,Toronto,416-555-5555;"

So the delimiter is a comma with the final data having a semicolon at
the end.

Appreciate any help, thanks in advance.

It would be much easier in my opinion if you used std::string instead of
char arrays.

But anyway...

Your comments state that those functions "retrieve" the name, city, etc. I
don't see either a parameter or return value for accepting the results.
Where do you intend to store the city, state, etc.? You need either a
parameter to hold the desired field, or a return value of some sort. You
also say "after that, I will strcpy the data", but after what? After
breaking up the string? Well, that involves strcpy already. Or did you
meant that those functions would return an index into the string, and then
you'd use that index for some strcpy operations?

Here's what I'd do (assuming I had to use char arrays): I'd write a function
to find a given character in a given array. I'd call it once to find the
comma at the end of the first name, then break up the string so I have first
name and the rest remaining. Then I'd call that function again on the
remaining string, to get the index of the end of the last name. (Or is that
order reversed? I can't tell whetner Harry or James is the last name!) I'd
just repeat that, passing the function a semicolon instead of a comma when
retrieving the phone number field.

And unless you can *guarantee* that the fields will always have data in
them, and in the expected order, make sure to write your code to be able to
handle an unexpected end of the string or an empty field.

-Howard
 
A

AMT2K5

If I was not clear ahead.

For example; void getLastName(char st[]) - copies the lastName portion
fo the data held in customer to st. For example, fi the data in
customer is "James,Harry,Toronto,416-555-5555;", getLastName will copy
"James" to st.

So for example, if I entered the following code in void
getLastName(char st[]),
puts(getCustomer());,

"James,Harry,Toronto,416-555-5555;" is shown in the console window.
 
A

AMT2K5

Ok I got the following to work so far for one category.

void Account::getLastName(char st[]) //Retrieves the last name
{
char temp[35];
strcpy(temp,getCustomer());

char *tokenPTR;
tokenPTR = strtok(temp, ",");
strcpy(st,temp);
}
 
H

Howard

AMT2K5 said:
Ok I got the following to work so far for one category.

void Account::getLastName(char st[]) //Retrieves the last name
{
char temp[35];
strcpy(temp,getCustomer());

char *tokenPTR;
tokenPTR = strtok(temp, ",");
strcpy(st,temp);
}

I forgot about strtok. That makes things much easier. (But you don't need
tokenPTR at all in this particular function, since you don't use it.)

But why have all those functions? (Is it a requirement, so that they can be
called at any time and in any order?)

If you're just going to break the customer into four strings, then all you
need to do is loop until strtok returns NULL, copying the return value into
your desired fields each time, and passing NULL as the first parameter for
the second, third and fourth calls. (And for the last call, at least, you
need the ; delimiter instead of or in addition to the , delimieter.)

If you need to be able to call any of those functions at any time, then you
do need the functions (obviously), and they can each loop the appropriate
number of times until they get to the field they need.

-Howard
 
A

AMT2K5

Thanks for the suggestions.
Question - Can the above be done with utilizing many different incoming
strings instead of working out one constant string?
 
D

Default User

AMT2K5 said:
If I was not clear ahead.


Please quote a relevant portion of the previous message when replying.
To do so from the Google interface, don't use the Reply at the bottom
of the message. Instead, click "show options" and use the Reply shown
in the expanded headers.

Brian
 
D

Default User

Howard said:
AMT2K5 said:
Hello, how would I go about breaking up a string that is returned by a
function. After I do that, I will strcpy that data to a class data
member [which I know how].

I have the following functions

void Account::getPhoneNumber(char st[]) //Retrieves the phone number


Your comments state that those functions "retrieve" the name, city, etc. I
don't see either a parameter or return value for accepting the results.
Where do you intend to store the city, state, etc.?

The parameters to those functions are char*, presumbably to point to
buffers of sufficient size to hold the results. It would be safer to
also pass in the size of the buffer to prevent overflow. Right now it's
on faith.

As you say, std::string would be better. Programmers, especially
newbies, should only use old-style arrays for very specific purposes
and only if they know what they are doing. Or if it's homework, which I
think is the case here. If not, the OP should get rid of the char
buffers.


Brian
 
A

AMT2K5

Let me know if this makes sense. What I want to do is the following.

I have a string like, "James,Harry,Toronto,416-555-5555;"

For the first name. I want to loop through the string and scan each
character until it hits a comma, I want to copy everything before the
comma.

For the last name. I want to loop through the string and scan each
character until it hits a comma, I want to copy everything from after
that comma until it hits the next comma.

For the city. I want to loop through the string and scan each character
until it hits a comma and than a second comma, copy everything after
that second comma until it hits the next comma.

For the phone number. I want to loop through the string and scan each
character until it hits a comma, and than a second and third comma,
copy everything after that last comma until it hits a semi-colon.

To do the above, the syntax is confusing me - I need some help with the
code.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top