Page 1 of 1

Convert string into array of ints

Posted: November 27th, 2014, 2:46 pm
by George_99
So i have a string (example : "1 12 69 123") and i want to put each of the space separated integers into an array of integers . the number of integers is known ( in this case 4 ) . Thanks in advance :3

Ps . i know it's not relevant to the framework . don't be too mad :(

Ps2 . array of std::vector is also acceptable

Re: Convert string into array of ints

Posted: November 27th, 2014, 4:37 pm
by cameron
unsigned int NDigits( int num )
{
return log10(num) + 1;
}

char buffer[100] = "(your string here)";
std::vector<int> nums;
unsigned int index = 0;
const unsigned int size = strlen(buffer);
while(index < size)
{
nums.push_back(atoi(&buffer[index]));
index += (NDigits(nums.back()) + 1);
}

Note you must include math.h

Just curious are you in a programming class?
Try that out, good luck!