1,105,578 Community Members

Converting a String into an integer array

Member Avatar
champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
andor
Posting Whiz in Training
273 posts since Jun 2005
Reputation Points: 25 [?]
Q&As Helped to Solve: 29 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Member Avatar
champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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]);
      }
   }
Member Avatar
FC Jamison
Posting Pro in Training
447 posts since Jun 2004
Reputation Points: 16 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
Team Colleague
 
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);
Member Avatar
champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Bench
Posting Pro
575 posts since Feb 2006
Reputation Points: 212 [?]
Q&As Helped to Solve: 64 [?]
Skill Endorsements: 0 [?]
 
1
 

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!

Member Avatar
champos
Newbie Poster
4 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

just c

Member Avatar
Grunt
Junior Poster
147 posts since Jul 2006
Reputation Points: 19 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

Look into sscanf()

Member Avatar
FC Jamison
Posting Pro in Training
447 posts since Jun 2004
Reputation Points: 16 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
Team Colleague
 
1
 

Could he use something like

static_cast<int>(string[0]) - 48
Member Avatar
Grunt
Junior Poster
147 posts since Jul 2006
Reputation Points: 19 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
1
 

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

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
1
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article