hi everybody,
how can I split my text into the array?
for example

mytext = "1 2 3 4 5 6";
mytext2 = splitFunction(mytext);

mytext2[0] = 1;
mytext2[1] = 2;
mytext2[2] = 3;

Recommended Answers

All 6 Replies

So what have you tried - or even read about which might suggest what you've tried.

I can't even guess as to which language you want this in as yet.

Of course, I read some solutions but they were all related with using vectors about which I have no any experience, can anyone suggest me more simple method to do this in c++?

I don't want to state the obvious but:

char mytext[5] = {'1','2','3','4','\0'};

mytext[0] == 1
mytext[1] == 2
Or am I missing your point here? I've had quite a mind-destroying week so it could be...

p.s. Happy newyear everybody

thanks for your try but
I have a string variable mytext whose content is for example
15 20 25 33 45 55

how can I reach this numbers specifically?
please help me....

You should go trough the chararray (string) with a loop and everytime when you encounter a "space" convert the previous chars to int and store them in an int-array. Something like this perhaps:

int Output[100];
char Buffer[10]; //max 10 digits
int i = 0;
int k = 0;
for (iCount = 0; mytext[iCount] != '\0'; iCount++)
   if (mytext[iCount] != ' ')
        Buffer[i] = mytext[iCount];
          Buffer[i] = '\0';
          Ouput[k] = atoi(Buffer);
          i = 0;

I haven't tested this because my compiler won't start... But what I'm trying to do is: Go trough the string with a loop until I encouter the 'end of string char ( \0 ). Store the value of the element in a buffer unless it's a space. If it's a space, terminate the string, convert the buffer to an int and store it in an int-array (output[]). Then update all counters and continue.
I hope you get the basic idea. There are probably more direct ways to solve your problem, but I'm not yet back to my logic self...

commented: niek, you're very helpful :) -joeprogrammer +4

it works well, thanks a lott.
have a nice day

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.