Split const char*

Hi,

How to split const char* into two const char*.
I have one const char* with + in between which need to split.
Eg: abcd+efgh in two const char* abcd & efgh.

Thanks
If you store the string in a non-const char array you can use strtok.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstring>

int main()
{
	char str[] = "abcd+efgh";
	
	const char* first  = std::strtok(str,     "+");
	const char* second = std::strtok(nullptr, "+");
	
	std::cout << first  << std::endl;
	std::cout << second << std::endl;
}
abcd
efgh

http://www.cplusplus.com/reference/cstring/strtok/
Last edited on
Thanks Peter :)
Topic archived. No new replies allowed.