Reputation: 1527
More specifically, how do I change'
char tempList[256] = "1 -2 -8 4 5";
into something like this:
int tempListNum[256] = {1, -2, -8, 4, 5};
?
I tried this, but I don't know how to append the array.
for (int j = 0; j < 256; j++)
{
if(TEMPS[j] == 45 && (TEMPS[j+1] >= 48 && TEMPS[j+2] >= 48))
{
numToAppend = ((TEMPS[j+1]-'0')*10 + (TEMPS[j+2]-'0')) * -1;
}
if(TEMPS[j] == 45 && TEMPS[j+1] >= 48)
{
numToAppend = (TEMPS[j+1]-'0') * -1;
}
if(TEMPS[j] >= 48)
{
numToAppend = TEMPS[j]-'0';
}
if(TEMPS[j] >= 48 && TEMPS[j+1] >= 48)
{
numToAppend = TEMPS[j]*10 + TEMPS[j+1];
}
}
Upvotes: 2
Views: 116
Reputation: 1981
use this: it works on me.
#include <stdio.h>
#include <ctype.h>
#include <math.h>
int main() {
char tempList[256] = "1 -2 -82 43 5";
char tempNumber[10];
int tempNumberCounter = 0;
int i, j,counter=0, isNegative = 1;
int numTempList[256] = {0};
for(i=0; tempList[i] != '\0'; i++) {
if(tempList[i-1] == '-') {
isNegative = -1;
}
if(isdigit(tempList[i])) {
tempNumber[tempNumberCounter] = tempList[i];
tempNumberCounter++;
}
if(tempList[i] == ' ' || tempList[i+1] == '\0') {
tempNumber[tempNumberCounter] = '\0';
for(j=0; j<strlen(tempNumber); j++) {
numTempList[counter] += (tempNumber[j] - '0') * pow(10, strlen(tempNumber)-j-1);
}
numTempList[counter] *= isNegative;
counter++;
tempNumberCounter = 0;
*tempNumber = 0;
isNegative = 1;
}
}
for(i=0; numTempList[i] != 0; i++) {
printf("%d ", numTempList[i]);
}
return 0;
}
numTempList
is the converted array from tempList
. this is just a sample implementation of char to int array, you can refactor this into functions so that you can reuse this functionality..
Now it can also read multiple digits
Upvotes: 0
Reputation: 2643
You can try this :
char tempList[256] = "1 -2 -8 4 5";
int tempListNum[256]
int i=0,k=0;
while(i<256)
{
int num=0;
for(int j=0;tempList[j]!=' ';j++)
{
int neg=0,counter=0; //Not worked in C for a long time and I am not sure if it supports Bools
if(a[j]=='-')
neg=1;
else
num=num*10+int(tempList[j])-int('0');
counter++;
}
if(neg==1)
num=-num;
tempListNum[k]=num;
k++; i+=counter;
}
Upvotes: 0
Reputation: 6407
Consider this short example:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char tempList[256] = "1 -2 -8 4 5";
int tempListNum[256] = {0};
int numCnt = 0;
char * pEnd = tempList;
int number = 0;
while( strlen(pEnd) > 0)
{
tempListNum[numCnt] = strtol (pEnd,&pEnd,0);
numCnt++;
}
for(number = 0; number < numCnt; number++)
{
printf("%d ", tempListNum[number]);
}
return 0;
}
Upvotes: 0
Reputation: 6407
For example, with while loop and functions sscanf and strtok from strings.h
.
#include <stdio.h>
#include <string.h>
int main (void)
{
char tempList[256] = "1 -2 -8 4 5";
int tempListNum[256] = {0};
int cnt = 0;
int tmp = 0;
char * str = strtok (tempList," ");
while(str != NULL)
{
if(sscanf(str, "%d", &tmp))
{
tempListNum[cnt] = tmp;
cnt++;
}
// read next number
str = strtok (NULL," ");
}
// test output
for(tmp = 0; tmp < cnt; tmp++)
{
printf("%d ", tempListNum[tmp]);
}
return 0;
}
Tested in Visual Studio 2012.
Upvotes: 0
Reputation: 597
Is this a homework assignment, or maybe a interview question?
I haven't tested this, but you could do something like this:
#define enum _error {
success = 0,
parse_error,
out_of_memory,
invalid_arg,
insuficient_buffer
} error;
error parse_int(
char **pp,
int *val)
{
error e = success;
char neg = 0;
if (!pp || !*pp || !val) {
e = invalid_arg;
goto exit;
}
*val = 0;
while (**pp) {
if (**pp == '-') {
if (val) {
e = parse_error;
goto exit;
}
neg = 1;
}
else if (**pp >= '0' **pp <= '9') {
*val *= 10;
*val += **pp - '0';
}
else {
break;
}
++*pp;
}
if (neg) {
*val *= -1;
}
exit:
return e;
}
error parse_array_of_ints(
char *p,
unsigned int parsed_ints_size,
int **parsed_ints)
{
error e = success;
unsigned int cur = 0;
if (!p || !parsed_ints) {
e = invalid_arg;
goto exit;
}
for (;;) {
while (*p && *p != '-' && *p < '0' && *p > '9') {
p++;
}
if (!*p) {
break;
}
if (cur = parsed_ints_size) {
e = insuficient_buffer;
goto exit;
}
e = parse_int(&p, parsed_ints+cur);
if (e != success) {
goto exit;
}
cur++;
}
exit:
return e;
}
Upvotes: 0
Reputation:
This isn't terribly difficult. I'd recommend a custom strtoi
function instead of atoi
since atoi
allows for little error handling. For example, atoi("1.3")
would return 1 with no other way to get other information like the fact that ".3" makes it not an integer. Moreover, the value 1 is obviously within the range of the int
type, so even errno
may not be of help.
That is why I write and use strtoi
below, which uses the C library's strtol
function and has the same behaviors. If you need help understanding what strtoi
does, look up documentation for strtol
since they're pretty much the same except for the range of valid values (between INT_MIN
and INT_MAX
, both inclusive, instead of between LONG_MIN
and LONG_MAX
, both inclusive).
#include <stdio.h>
#include <stdlib.h> //strtol
#include <string.h> //strtok
#include <limits.h> //INT_MIN, INT_MAX
#include <errno.h> //errno, ERANGE
int
strtoi (const char *s, char **rem, int base)
{
long n;
int esave = errno;
errno = 0;
n = strtol (s, rem, base);
if (n < INT_MIN || n > INT_MAX)
errno = ERANGE;
if (errno)
{
if (n == LONG_MAX)
return INT_MAX;
return INT_MIN;
}
errno = esave;
return (int) n;
}
int
main (void)
{
char tempList[256] = "1 -2 -8 4 5";
int tempListNum[128] = {0};
int i, n;
char *listPtr;
char *listPtr2;
listPtr = strtok (tempList, " ");
for (i = 0;
listPtr != NULL && (size_t) i < sizeof tempListNum / sizeof tempListNum[0];
++i)
{
errno = 0;
n = strtoi (listPtr, &listPtr2, 10);
if (*listPtr2)
{
fprintf (stderr, "error: value `%s' is not an integer ... ignoring value\n", listPtr);
--i;
}
// value out of range or some other implementation-defined error
else if (errno)
{
perror (listPtr);
}
listPtr = strtok (NULL, " ");
}
// n is now the number of items in the array
n = i;
for (i = 0; i < n; ++i)
printf ("%d\n", tempListNum[i]);
}
You could obviously avoid strtok
and just use strtoi
by itself. That might actually be a cleaner solution!
Upvotes: 0
Reputation: 1032
you can use strtok with space as separator and then use atoi standard library function
Upvotes: 1
Reputation: 2547
You can try this
unsigned char index1 = 0,index2 =0;
int PrevValue = 0;
for(;index1 <= 255;index1 ++)
{
if(tempList[index1] != ' ') //space
{
tempListNum[index2] = PrevValue*10 + (tempList[index1]-0x30);
PrevValue = tempListNum[index2] ;
}
else
{
PrevValue = 0;
index2++;
}
}
You can add further optimizations and other validations.
Upvotes: 0
Reputation: 42828
Write a function that goes through tempList
char
by char
:
While it encounters only numbers, it should turn them into an int
. If there are more than 1 digit, it should multiply the first digit with 10
and add then add it to the int
, etc.
When it encounters a space, it should add the newly created int
to tempListNum
and skip the space.
When it encounters a minus sign, it should multiply the currently created int
with -1
.
Repeat these steps until end of list.
Upvotes: 0