fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main() {
  5. char str[100];
  6. const char* delim = " ";
  7.  
  8. gets(str);
  9.  
  10. int arr[100];
  11. char temp[100]=" 1 2 3 4 5 ";
  12. strcpy(temp, str);
  13. size_t idx=0;
  14.  
  15. char* pch=strtok(temp,delim);
  16. while(pch!=NULL) {
  17. arr[idx++] = atoi(pch);
  18. pch=strtok(NULL, delim);
  19. }
  20. // 精簡版
  21. // for(char* pch=strtok(temp,delim); pch; pch=strtok(NULL, delim))
  22. // arr[idx++] = atoi(pch);
  23.  
  24. for(unsigned i = 0; i < idx; ++i) {
  25. printf("%d, ", arr[i]);
  26. }printf("\n");
  27. return 0;
  28. }
Success #stdin #stdout 0s 9432KB
stdin
1 2 3 4 5 6 8 9 10
stdout
1, 2, 3, 4, 5, 6, 8, 9, 10,