You are here:

C++/split string


Followup To
Question -
sir i want to split this string by hiphen (-)
and store it in  two seperate variables.pls give me sample code as
soon as possible
Answer -
Hello Ankitjain, thank you for the question.

This problem turns out to be really simple when we use a function called "strtok()" which resides in <string.h>. It tokenizes our string based on a specified set of delimiters, in this case, the hyphen. This sample code should help you a good deal:

// in main

char str[]   = "";

char *first  = strtok(str, "-");

char *second = strtok(NULL, "\0");

After this code executes, first contains "" and second contains "". What happens is that we pass the string str to strtok and pass the hyphen as a delimiter. It returns everything up to the hyphen, which is the first half of the string, and it places a NULL terminator where it stops in the string. So we strtok again from NULL which is the current place in the string, using the NULL terminator as a delimiter so it will only search to the end of the string, and it stores the second half in the variable "second". If you have problems parsing strings in the future, I suggest looking up the functions "strchr(), strtok(), and strstr()" in the msdn for easy help. If you don't use Visual Studio, you can find it online at If you have any further questions please don't hesitate to ask me.

I hope this information was helpful.

- Eddie

Thanks for giving me soliution .it works well but if there are three hiphen
then i wrote

char *first  = strtok(str, "-");
char *second = strtok(NULL, "-");
char *third= strtok(NULL, "\0");
It also works well.but if hiphen are not present
ie if i give input like ,
then it gives memory leakage problem .compiler says could not read.
pls tell me how to remove this memory problem
thanks and regards

Hello again Ankitjain, thanks for the question.

You're not going to be able to make any kind of function out of this process. You need to know the exact string you're parsing and what you're looking for here. There cannot be a memory leakage problem here because no memory was ever allocated to be leaked in the first place. Remember how I told you that strtok puts NULL terminators in the string each time it stops parsing? Lets think of what's happening here for this:

char *first = strtok(str, "-");
char *second = strtok(NULL, "-");
char *third= strtok(NULL, "\0");

for the string: ,

We call strtok and get: Now we have , left.
We call strtok and no hyphen is found. We are at the end of "str" now. Now "str" is a NULL pointer.

We call strtok on a NULL pointer. Program crashes.

You have to be careful when parsing strings like this. Otherwise these problems start popping up all over the place. You have to know your string and what you hope to accomplish by calling these string functions on it, and what they do to manipulate the string.

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 All rights reserved.