Convert string into array of ints

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
George_99
Posts: 25
Joined: December 4th, 2013, 2:58 pm

Convert string into array of ints

Post by George_99 » November 27th, 2014, 2:46 pm

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

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Convert string into array of ints

Post by cameron » November 27th, 2014, 4:37 pm

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!
Computer too slow? Consider running a VM on your toaster.

Post Reply