473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split string whose length is varying

Say I have a string which contains numbers separated by a comma... such
as "0,1,2,3,4,5"...I want to split the string at the commas and return
an array containing, 0,1...5.

Suggestions? I've tried something like...

int *Split(char *msg) {
int len = 0;
char *tmp;
char *sub_string = NULL;
int *results;

tmp = (char *) malloc(strlen(msg) * sizeof(char));
strcpy(tmp, msg);

sub_string = strtok(tmp, ",");
while (sub_string != NULL) {
len++;
sub_string = strtok(NULL, ",");
}

results = (int *) malloc(len * sizeof(int));
sub_string = strtok(msg, ","); <<<<----crahses here
while (sub_string != NULL) {
*results = (int) atoi(sub_string);
results += sizeof(int);
sub_string = strtok(NULL, ",");
}

return results;
}

....above you can see I have pointed out where it crashes. I think it's
because strtok needs a pointer to the string...so I changed that line
to..

sub_string = strtok(&msg, ",");

...which got me past the error, but then when I try to print the results
after the function...it's not right. Maybe its my function or my way
of printing results.

main() {
char *msg = "1,2,3,4,5";
int *results = Split(msg);
// How do I print the results here??
}

Thanks in advance.

Nov 15 '05 #1
9 2475


Java and Swing wrote On 10/06/05 10:57,:
Say I have a string which contains numbers separated by a comma... such
as "0,1,2,3,4,5"...I want to split the string at the commas and return
an array containing, 0,1...5.

Suggestions? I've tried something like...

int *Split(char *msg) {
int len = 0;
char *tmp;
char *sub_string = NULL;
Why bother to initialize? You never make use of
the initialized value; it just gets overwritten the
first time you call strtok().
int *results;

tmp = (char *) malloc(strlen(msg) * sizeof(char));
strcpy(tmp, msg);
The classic C off-by-one error: your `tmp' region
is too small to contain `msg'. (You've also failed to
check for malloc() failure, and you've created a memory
leak by not free()ing `tmp' when you're done with it.)
sub_string = strtok(tmp, ",");
while (sub_string != NULL) {
len++;
sub_string = strtok(NULL, ",");
}
This loop just counts the number of commas in `msg'.
Well, almost: if the input held ",1,,3," this loop
would count two, not four -- but if you don't need to
worry about that sort of thing, there are simpler ways
to obtain the count.
results = (int *) malloc(len * sizeof(int));
sub_string = strtok(msg, ","); <<<<----crahses here
Remember, strtok() modifies the string it is scanning.
You've handed it a pointer to a string literal, which must
be treated as a constant: any attempt to modify the string
causes undefined behavior.
while (sub_string != NULL) {
*results = (int) atoi(sub_string);
Why cast an int to an int? Also, atoi() is not a
good way to convert strings to numbers: it will do
unpredictable things if given strings like "abba", and
its behavior on strings like "23skiddoo", although
predictable, leaves you unaware of the strange input.
Try strtol() instead.
results += sizeof(int);
This is horribly wrong. You need to go back to your
textbook and review what it says about pointer arithmetic.
sub_string = strtok(NULL, ",");
}

return results;
}

...above you can see I have pointed out where it crashes. I think it's
because strtok needs a pointer to the string...so I changed that line
to..

sub_string = strtok(&msg, ",");
This is wrong, too, and your compiler should have
complained about it -- unless, perhaps, you forgot to
#include <string.h>, which would be yet another error.

The practice of making random changes in hopes that
a bug you don't understand will somehow disappear is not
the use of reason but of magic. Even if you do get the
program to work for the test cases you throw at it, you
won't understand why it works and you won't know whether
it works only by the strangest of unstable coincidences.
Maybe it only works if the last number happens to be odd,
or if there's an even number of numbers altogether, or
if the moon is waning. The technique is Not Recommended.
..which got me past the error,
... by introducing a completely different one ...
but then when I try to print the results
after the function...it's not right. Maybe its my function or my way
of printing results.

main() {
char *msg = "1,2,3,4,5";
int *results = Split(msg);
// How do I print the results here??
}

Thanks in advance.


Nov 15 '05 #2
thanks for the input, i will try to make changes as you suggested.
what simpler ways are there to count the number of commas in msg?

any good online tutorials? ...its been quite a few years since i've
done C.

thanks.

Nov 15 '05 #3
Java and Swing <co*******@gmail.com> wrote:
thanks for the input, i will try to make changes as you suggested.
what simpler ways are there to count the number of commas in msg?


It is proper Usenet etiquette to include the text you are replying to.
To do this using Google groups, please follow the instructions below,
penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

(That said, assuming count is an int and cp is a char *:

count=0;
while( (cp=strchr(msg,',')) != NULL ) {
count++;
cp++;
}

is much better for counting commas.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #4
when i try..

void count(char *msg) {
int count = 0;
char *cp;

while ( (cp = strchr(msg, ',')) != NULL ) {
printf("cp = %s\n", cp);
count++;
cp++;
}

printf("Count: %d", count);
}

main() {
char *msg = "1,2,4,5";
count(msg);
}

...the output i get is , ",2,4,5" forever.

Christopher Benson-Manica wrote:
Java and Swing <co*******@gmail.com> wrote:
thanks for the input, i will try to make changes as you suggested.
what simpler ways are there to count the number of commas in msg?


It is proper Usenet etiquette to include the text you are replying to.
To do this using Google groups, please follow the instructions below,
penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

(That said, assuming count is an int and cp is a char *:

count=0;
while( (cp=strchr(msg,',')) != NULL ) {
count++;
cp++;
}

is much better for counting commas.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


Nov 15 '05 #5
i fixed it...

void count(char *msg) {
int count = 0;
char *cp;
char *bak = (char *) malloc((strlen(msg) * sizeof(char)) + 1);
strcpy(bak, msg);

while ( (cp = strchr(bak, ',')) != NULL ) {
printf("cp = %s\n", cp);
count++;
bak++;
}
printf("Count: %d", count);
}

Java and Swing wrote:
when i try..

void count(char *msg) {
int count = 0;
char *cp;

while ( (cp = strchr(msg, ',')) != NULL ) {
printf("cp = %s\n", cp);
count++;
cp++;
}

printf("Count: %d", count);
}

main() {
char *msg = "1,2,4,5";
count(msg);
}

..the output i get is , ",2,4,5" forever.

Christopher Benson-Manica wrote:
Java and Swing <co*******@gmail.com> wrote:
thanks for the input, i will try to make changes as you suggested.
what simpler ways are there to count the number of commas in msg?


It is proper Usenet etiquette to include the text you are replying to.
To do this using Google groups, please follow the instructions below,
penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

(That said, assuming count is an int and cp is a char *:

count=0;
while( (cp=strchr(msg,',')) != NULL ) {
count++;
cp++;
}

is much better for counting commas.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


Nov 15 '05 #6
char *bak = (char *) malloc((strlen(msg) * sizeof(char)) + 1);


There is no need (and in fact it can hide errors) to cast malloc's
return value, the reason your compiler is complaining if you dont have
a cast is because you didnt include stdlib.h.

deaden

Nov 15 '05 #7
Java and Swing <co*******@gmail.com> wrote:
i fixed it... while ( (cp = strchr(bak, ',')) != NULL ) {
printf("cp = %s\n", cp);
count++;
bak++;
}


Well, it's certainly gratifying to see that you grasped the concept
well enough to fix the bug in my code. :-)

I hate when I do that.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #8
however, i did realize that the code does not count the number of
tokens. it counts the number of chracters minus 1.

for example, pass it, "1,2,3,4" and you get back count = 6. I changed
to use strtok and have it working fine now.

Christopher Benson-Manica wrote:
Java and Swing <co*******@gmail.com> wrote:
i fixed it...

while ( (cp = strchr(bak, ',')) != NULL ) {
printf("cp = %s\n", cp);
count++;
bak++;
}


Well, it's certainly gratifying to see that you grasped the concept
well enough to fix the bug in my code. :-)

I hate when I do that.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


Nov 15 '05 #9
Java and Swing <co*******@gmail.com> wrote:
however, i did realize that the code does not count the number of
tokens. it counts the number of chracters minus 1. for example, pass it, "1,2,3,4" and you get back count = 6. I changed
to use strtok and have it working fine now.


I'm not doing myself any favors here (I already look like a dork), but
the *real* fix should be

cp=msg;
count=0;
while( (cp=strchr(cp,',')) != NULL ) {
count++;
cp++;
}

Do you see why you got 6 with
while ( (cp = strchr(bak, ',')) != NULL ) {
printf("cp = %s\n", cp);
count++;
bak++;
}


? (Try to figure it out - if you get it, then you really get this now.)

In any case, do NOT use strtok() for counting the commas - I
promise you it can be done better without it, just please overlook the
fact that it took me three times to get it right...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #10

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

Similar topics

5
31156
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
29797
by: | last post by:
Hello everyone! :-D OK, I've came across many functions for this, but none works! I need one that works like this: StringSplit(string str, string delim, vector<string> results) Either with...
5
6084
by: bu | last post by:
I have a database field that represents an item description and varying length. I am using the database to import the item description into another program (the other program is ODBC-aware ). The...
4
728
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
3
9642
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
2
2172
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
5
2874
by: sck10 | last post by:
Hello, I have a list of email addresses that I need to send email to from the website. I am trying to use the "Split" function to get all the To's and then use the uBound function for the...
1
8352
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
14
1705
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
0
7201
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,...
0
7328
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...
1
6988
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
7456
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
5578
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,...
1
5011
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...
0
4672
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...
0
3166
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...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.