943,974 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9931
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 20th, 2006
0

Converting a String into an integer array

Expand 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
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
champos is offline Offline
4 posts
since Sep 2006
Sep 20th, 2006
1

Re: Converting a String into an integer array

Click to Expand / Collapse  Quote originally posted by champos ...
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.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 20th, 2006
0

Re: Converting a String into an integer array

Click to Expand / Collapse  Quote originally posted by 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]);
}
}
Reputation Points: 14
Solved Threads: 0
Newbie Poster
champos is offline Offline
4 posts
since Sep 2006
Sep 20th, 2006
0

Re: Converting a String into an integer array

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.
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 20th, 2006
0

Re: Converting a String into an integer array

Click to Expand / Collapse  Quote originally posted by FC Jamison ...
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.
Reputation Points: 14
Solved Threads: 0
Newbie Poster
champos is offline Offline
4 posts
since Sep 2006
Sep 20th, 2006
1

Re: Converting a String into an integer array

Click to Expand / Collapse  Quote originally posted by champos ...
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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Sep 20th, 2006
1

Re: Converting a String into an integer array

just c
Reputation Points: 14
Solved Threads: 0
Newbie Poster
champos is offline Offline
4 posts
since Sep 2006
Sep 20th, 2006
0

Re: Converting a String into an integer array

Look into sscanf()
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 20th, 2006
1

Re: Converting a String into an integer array

Could he use something like

  1.  
  2. static_cast<int>(string[0]) - 48
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 20th, 2006
1

Re: Converting a String into an integer array

Click to Expand / Collapse  Quote originally posted by FC Jamison ...
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.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: using strtok() to populate and array
Next Thread in C Forum Timeline: converting int to char in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC