473,406 Members | 2,220 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,406 software developers and data experts.

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

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.

Jul 23 '05 #1
9 2670

"AMT2K5" <Aa*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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


Jul 23 '05 #2
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.

Jul 23 '05 #3
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);
}

Jul 23 '05 #4

"AMT2K5" <Aa*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Jul 23 '05 #5
Thanks for the suggestions.
Question - Can the above be done with utilizing many different incoming
strings instead of working out one constant string?

Jul 23 '05 #6


AMT2K5 wrote:
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

Jul 23 '05 #7


Howard wrote:
"AMT2K5" <Aa*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Jul 23 '05 #8
This is part of an assignment where I am given the used function
definitions.

Jul 23 '05 #9
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.

Jul 23 '05 #10

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

Similar topics

2
by: Bengt Richter | last post by:
Why wouldn't quote-stuffing solve the problem, and let you treat \ as an ordinary character? In a raw string, it's no good for preventing end-of-quoting anyway, unless you want the literal \ in...
4
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). ...
5
by: Christopher Benson-Manica | last post by:
The function in question follows: vector<string>& tokenize( const string& s, vector<string>& v, char delimiter=',' ) { int delim_idx, begin_idx=0, len=s.length(); for(...
10
by: Alex | last post by:
I'm looking for a fast way to split a string into individual tokens. The typical format of the string is token1|token2|token3|...|tokenN| and the number of tokens varies (which is why i use a...
3
by: Rico | last post by:
If there are consecutive occurrences of characters from the given delimiter, String.Split() and Regex.Split() produce an empty string as the token that's between such consecutive occurrences. It...
7
by: Felix85 | last post by:
I am trying to make a command interpreter for a mud that i am working on the problem i am having right now is that i cannot convert the string into a char array. This is the error I am getting...
10
by: Bilal | last post by:
Hello, I'm trying to perform some string manipulations in my stylesheet and have gotten stuck on the issue below so hopefully can elicit some useful hints. Namely, the problem is that I need to...
16
by: Amit Gupta | last post by:
Hi - I get a seg-fault when I compile and run this simple program. (seg-fault in first call to strtok). Any clues? My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)" #include...
4
by: nnguyec | last post by:
Hi, I'm trying to write a small code for an assignment which the void String Tokenizer will get a line input, and take out those delimiters from the original string. Then pass each string without the...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.