[Solved] Splitting a string into integers


Since you are sure the string will contain digits, subtract each character from '0' to get their numeric value.

int sum = 0;
fgets(str, sizeof str, stdin);
char *s = &str;
while(*s != '\0') {
    sum += *(s++) - '0';
}

printf("the sum of the digits is: %d", sum);

solved Splitting a string into integers