Oskar posted:
Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323
dvjiv 234") and i want to put the data (space separated) into an
array.It shuld be somethign like this.
array1[0]="aaa";
array1[1]="bbb";
...
array1[43653]="234";
The thing is i don`t have any clue how to do it. :|
Thx for help
Why not just write a function?
First of all, find out how many spaces there are.
Then define an array via new. Then just take the addresses.
Untest code but you'll get the jist:
char* ReturnStringArray(const char* input_string)
{
size_t length = strlen(input_string);
size_t amount_spaces = 0;
for(size_t i = 0; i < length; ++i)
{
if (input_string[i] == ' ') ++amount_spaces;
}
char* const p_stringarray = new char*[amount_spaces + 1];
p_stringarray[0] = input_string;
unsigned short index = 1;
for(size_t i = 0; i < length; ++i)
{
if (input_string[i] == ' ')
{
p_stringarray[index++] = input_string[i+1];
}
}
p_stringarray[index] = 0;
}
int main()
{
char* p_blah = "aaaa bbbb cccc ffff kkkk";
char* p_stringarray = ReturnStringArray(p_blah);
//Work with it
delete [] p_stringarray;
}
-JKop