用c++写,新建字符串 a = “5,0,1,2,10,0,51”,提取出7个数字
时间: 2024-05-05 16:18:52 浏览: 13
Sure, I can help you with that. Here's the code to extract the 7 numbers from the given string:
```c
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "5,0,1,2,10,0,51"; // the given string
int nums[7]; // array to store the extracted numbers
char *token; // pointer to hold each token
int i = 0;
token = strtok(a, ",");
while(token != NULL) {
nums[i++] = atoi(token); // convert token to integer and store in array
token = strtok(NULL, ",");
}
// print the extracted numbers
for(int j = 0; j < 7; j++) {
printf("%d ", nums[j]);
}
return 0;
}
```
Output:
```
5 0 1 2 10 0 51
```