string vs Byte

Hi All,

I am new in c++ please tell me how we can convert string into array


satish
Hope the following code is helpful for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

char **to_array (char *str)
{
	static char **str_arr = NULL;
	static char *cstr = NULL;
	if(str_arr)
	{
		free(str_arr);
		str_arr = NULL;
	}

	if(cstr)
		free(cstr);

	cstr = (char *)calloc(strlen(str)+1, sizeof(char));
	strcpy (cstr, str);

	char *p;
	int n = 0;
	p=strtok (cstr," ");
	while (p!=NULL)
	{
		n++;
		if(str_arr == NULL)
			str_arr = (char **)malloc(sizeof(char *));
		else
			str_arr = (char **)realloc(str_arr, n * sizeof(char *));
		str_arr[n - 1] = p;

		p=strtok(NULL," ");
	}
	str_arr = (char **)realloc(str_arr, (n+1) * sizeof(char *));
	str_arr[n] = NULL;

	return str_arr;
}

int main ()
{
	char **str_arr ;
		cout << "----------" << endl;
	str_arr = to_array("Please split this phrase into tokens");
	while(*str_arr)
	{
		cout << *str_arr << endl;
		str_arr++;
	}
		cout << "----------" << endl;
	str_arr = to_array("Please split this phrase into tokens");
	while(*str_arr)
	{
		cout << *str_arr << endl;
		str_arr++;
	}

	return 0;
}

@ zhongnan - did you read the question, or have you posted in the wrong thread??
Topic archived. No new replies allowed.