954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting a String into an integer array

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

champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 
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
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
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]);
}
}

champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 

Correct me if I'm wrong...but doesn't atoi() require a C-String as the argument?

char input[20];

code...code...code...

atoi(input);
FC Jamison
Posting Pro in Training
Team Colleague
446 posts since Jun 2004
Reputation Points: 92
Solved Threads: 21
 
Correct me if I'm wrong...but doesn't atoi() require a C-String as the argument?



it does. i wanted to see if it is possible to send a single part of the array string. ie string[0] then increment to string[1] and so on.

champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 
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 library - stringstreams make this sort of thingreally, really easy!

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 
champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 

Look into sscanf()

Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 

Could he use something like

static_cast<int>(string[0]) - 48
FC Jamison
Posting Pro in Training
Team Colleague
446 posts since Jun 2004
Reputation Points: 92
Solved Threads: 21
 

Could he use something like

static_cast<int>(string[0]) - 48


I think you should avoid casts as much as possible unless there's no option left or it turns out to be less evil then the other option.

Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 

In order of preference
strtol() - does conversion and will report numeric overflow / underflow and tell you how much data was processed
sscanf() - like strtol() without the over/under flow
atoi() - pretty useless atoi("0") and atoi("hello") may both return 0

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You