Reputation: 11
I have 2 functions, they are called SplitInt(unsigned int x)
and SplitChar(char x[])
SplitInt
takes in any positive integer, such as 12345
, and puts each digit into an integer array, but backwards. So for SplitInt(12345)
the array would look like this:
array[0] = 5
array[1] = 4
array[2] = 3
array[3] = 2
array[4] = 1
SplitChar()
is supposed to do the same thing but take in a C-Style string, such as "12345"
.
How would I separate the individual digits and send them into an integer array? I am not allowed to use string
class.
Thanks!
Upvotes: 0
Views: 106
Reputation: 408
#define MAX_STR_LENGTH 100
int array[MAX_STR_LENGTH];
int SplitChar(char x[])
{
int len = strlen( x );
if ( len > MAX_STR_LENGTH )
return -1;
int i = 0;
while ( i < len )
{
array[len-i-1] = x[i] - '0';
i++;
}
return len;
}
Returns the length of the string or -1 if too long.
Upvotes: 1