fork download
  1. //exercise 1: a simple C program
  2. //http://e...content-available-to-author-only...u.tw/~klim/unix-p/usp-1092.html
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. int main() {
  7. char *ptr, c;
  8. char *str = malloc(sizeof(char) * 256 + 1);
  9. char *num = malloc(sizeof(char) * 16 + 1);
  10. int i = 0, j = 0, len;
  11. long sum = 0;
  12.  
  13. while ((c = getchar()) != EOF) {
  14. str[i] = c;
  15. i++;
  16. }
  17. str[i] = '\0';
  18. for (i = 0; i <= strlen(str); i++) {
  19. num[j] = str[i];
  20. j++;
  21. if ((str[i] == 32) || (str[i] == '\n')) {
  22. num[j] = '\0';
  23. if ((num[0] == '\0') || (num[0] < 48) || (num[0] > 57)) {
  24. char *num = "0";
  25. }
  26. else {
  27. sum = sum + strtol(num, &ptr, 10);
  28. }
  29. j = 0;
  30. }
  31. }
  32. printf("\n%ld\n", sum);
  33. }
  34.  
Success #stdin #stdout 0s 4692KB
stdin
I have 1000
You have 200
I give you 50
Now You have 250.
stdout
1250