Converting a String into an integer array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 4
Reputation: champos is an unknown quantity at this point 
Solved Threads: 0
champos champos is offline Offline
Newbie Poster

Converting a String into an integer array

 
0
  #1
Sep 20th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Converting a String into an integer array

 
1
  #2
Sep 20th, 2006
Originally Posted by champos View Post
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 4
Reputation: champos is an unknown quantity at this point 
Solved Threads: 0
champos champos is offline Offline
Newbie Poster

Re: Converting a String into an integer array

 
0
  #3
Sep 20th, 2006
Originally Posted by andor View Post
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]);
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Converting a String into an integer array

 
0
  #4
Sep 20th, 2006
Correct me if I'm wrong...but doesn't atoi() require a C-String as the argument?

  1. char input[20];
  2.  
  3. code...code...code...
  4.  
  5. atoi(input);
Last edited by FC Jamison; Sep 20th, 2006 at 11:11 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 4
Reputation: champos is an unknown quantity at this point 
Solved Threads: 0
champos champos is offline Offline
Newbie Poster

Re: Converting a String into an integer array

 
0
  #5
Sep 20th, 2006
Originally Posted by FC Jamison View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: Converting a String into an integer array

 
1
  #6
Sep 20th, 2006
Originally Posted by champos View Post
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!
Last edited by Bench; Sep 20th, 2006 at 11:17 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 4
Reputation: champos is an unknown quantity at this point 
Solved Threads: 0
champos champos is offline Offline
Newbie Poster

Re: Converting a String into an integer array

 
1
  #7
Sep 20th, 2006
just c
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Converting a String into an integer array

 
0
  #8
Sep 20th, 2006
Look into sscanf()
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Converting a String into an integer array

 
1
  #9
Sep 20th, 2006
Could he use something like

  1.  
  2. static_cast<int>(string[0]) - 48
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Converting a String into an integer array

 
1
  #10
Sep 20th, 2006
Originally Posted by FC Jamison View Post
Could he use something like

  1.  
  2. 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.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 6767 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC