#### Archived

This topic is now archived and is closed to further replies.

# splitting strings

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi does anyone know how to split a string into 2 at the first occurance of a comma in c (no stl please)? i.e. First[256], Second[256]; TestString = "firststring,secondstring" split (TestString, First, Second, '','') First = "firststring" Second = "secondstring" the comma woundn''t be included in either of the strings i''ve been trying it out with tokens but the results seem a little unpredictable (the msdn help file for strtok isnt much help either) thanks

##### Share on other sites
void split(char * input, char * first, char * second, char splitchar) {

int inputSize = (int)strlen(input);
int firstStringPos = 0;
int secondStringPos = 0;
first[0] = 0;
second[0] = 0;

int nextChar = 0;

for(; nextChar < inputSize; nextChar++);
if(input[nextChar] == splitchar) break;
first[firstStringPos++] = input[nextChar];
first[firstStringPos] = 0; // just to null terminate it each time...
};

for(; nextChar < inputSize; nextChar++) {
second[secondStringPos++] = input[nextChar];
second[secondStringPos] = 0;
};

};

.... I hope this works I cannot test it, lol!

www.cppnow.com

##### Share on other sites
keep in mind that if the delimiter character is NOT found, second string will be "".

www.cppnow.com

##### Share on other sites
Oops a mistake.

add this statement BETWEEN the two for loops:

nextChar++;

...This will increment the position of the input string to omit the delimiter character (the "comma" in your example).

www.cppnow.com

##### Share on other sites
quote:
Original post by AINewbie

i''ve been trying it out with tokens but the results seem a little unpredictable (the msdn help file for strtok isnt much help either)

thanks

The strtok function is not very intuitive, but its probably the easiest way to do what you want in C. Try this,

char testString[20] = "Hello,world";
char *first, *second;

first = strtok(testString, '','');
second = strtok(NULL, "\0\r\n");

The first strtok sets the string to scan and the delimiter to stop at, and returns the first word. The second strtok remembers which string to scan and starts where the last strtok left off, hence the NULL, the second parameter tells strtok to stop at the end of the string, and returns the second word.

HTH

##### Share on other sites
strtok reuses existing strings, it seems.

In the original post, C++ source shows that he defined 2 separate strings first[256] and second[256].

I wrote the split function on the assumption that he wants 2 unique strings with their own allocations, and also without altering the original string, which I believe strtok does.

Don''t get me wrong I like strtok (I also find it confusing sometimes though too

www.cppnow.com

##### Share on other sites
his original code could always be modified to use strtok and just add a couple of strcpy''s to create the two seperate buffers (if he don''t mind loosin'' the odd comma or two )

i had a look at the strtok documentation, and yeah, it does seem a bit backwards in coming forwards. when i first read it i got so worried the countless warnings of how easy it was to corrupt the data that i abandoned them (ah, to be a newb again). just don''t go calling it with different strings in recursive functions )

##### Share on other sites
You can use strcspn() and strncpy()/strcpy() to get the same effect, if you're concerned about messing up the original or multithreading/recursive issues.

    const char *original = "Hello,There";char first[256], second[256];size_t comma_position;comma_position = strcspn(original, ",");// Make sure there's something on both sides of the commaif((comma_position > 0) && (original[comma_position + 1] != 0)){   // Copy up to comma in first string   strncpy(first, original, comma_position);   // Properly terminate first   first[comma_position] = 0;   // Copy remaining string past comma into second string   strcpy(second, &(original[comma_position+1]));}

I didn't test the above, I'll leave it as an exercise for the OP if it doesn't work

EDIT: Fixed first copy to properly terminate.

[edited by - cgoat on April 1, 2003 3:15:47 PM]

##### Share on other sites
Apparently, strcspn() returns the position of the null character if the character isn''t found. Take that into account as well.

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013556
×