convert char array to integer value and add them
How can I extract numbers from a char array, separated with spaces开发者_开发百科, convert them to integers and sum them? For example:
"34 54 3 23"
I'd start at the beginning of the array, check each character in turn with isdigit()
and keep a current value and a current total.
When reaching the terminating NUL char (or last element of the array), the current total is already calculated.
You need to parse the string.
If you know how many integers are in there, you could use just a sscanf
.
Otherwise find out where blanks are (with something similar to strtok
, for example) and then read integers using atoi
精彩评论