开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜