问题描述:

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

相关阅读:
Top