## Featured Replies in this Discussion

Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Thanks

Don't know is it the best way but you can do like this. Parse the string until the space (' ') and the number until the space convert to int element of array.

andor: Don't know is it the best way but you can do like this. Parse the string until the space (' ') and the number until the space convert to int element of array.

Thats what i thought i would be able to do but i couldn't get something like that to work. I am probably doing something wrong.

I thought something like below would do the trick but i keep getting cast errors.

```
while (*string != '\0')
{
for (i = 0; i < strlen(string); i++)
{
converted[i] = atoi((int)string[i]);
}
}
```

Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Thanks

Are you using C++ or C? if you're using C++ then investigate stringstreams, which can be found in the <sstream> library - stringstreams make this sort of thing *really, really* easy!

Maybe something like this is what you are looking for

```
int main (void)
{
int i, j ;
char num_string[] = "1 234 23 45" ;
char tmp [2] = {'\0', '\0'} ;
int length = strlen (num_string) ;
int* values = (int*) malloc (sizeof (int) * length) ;
for ( i = 0, j = 0; i < length; ++i)
{
if ( isdigit (num_string[i] ) )
{
tmp [0] = num_string [i] ;
values [j++] = atoi (tmp) ;
}
}
printf ("\nThe string: %s", num_string) ;
printf ("\nThe integer array which results is: ") ;
for ( i = 0; i < j; ++i)
printf (" %d ", values[i]) ;
return 0 ;
}
```

My output is:

The string: 1 234 23 45

The integer array which results is: 1 2 3 4 2 3 4 5

Press ENTER to continue.

Hope it helped , bye.