View previous topic :: View next topic |
Author |
Message |
pAces Guest
|
parsing function |
Posted: Fri Apr 28, 2006 5:28 pm |
|
|
I have a string "M 1 2 3 4 5 6 7 8 9" that I got from the function gets() (serial communication). Is there a parse function in ccs c that will split the numbers (1,2,3,4..) and put them in an int 9 elements array? |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Apr 29, 2006 7:53 am |
|
|
I don't think there is a function that will do all you want, but have a look at the standard C function strtok(), this comes very close. Strtok will split your string into 'tokens', giving you a pointer to the first token and on every subsequent call a pointer to the next token. At first use I found this function a bit confusing but study the example code in ex_str.c or search the internet for more documentation.
To use strtok() you will have to include <string.h> in your program. |
|
![](templates/subSilver/images/spacer.gif) |
KamPutty Guest
|
Re: parsing function |
Posted: Sat Apr 29, 2006 10:17 am |
|
|
pAces wrote: | I have a string "M 1 2 3 4 5 6 7 8 9" that I got from the function gets() (serial communication). Is there a parse function in ccs c that will split the numbers (1,2,3,4..) and put them in an int 9 elements array? |
Just traverse the initial string starting at element 2 [0 based] (the "1"), use the Ascii to string function (atoi) to convert to an int. Then increase the index by 2 and repeat. This is all assuming the string is one space and one digit.
~Kam (^8* |
|
![](templates/subSilver/images/spacer.gif) |
|