Given a string, which consists of numbers, find the sum of the numbers.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Convert the character digit to a number */
#define toindex(x) ((int)x-(int)'0')
int main(void) {
char *s;
int len=0,sum=0;
/* Allocate memory for the input string */
s=(char*)malloc(sizeof(char)*256);
/* Get the input string from the user */
scanf("%s",s);
/* Until the pointer reaches the end of the string */
while(*s)
{
/* Sum the integer values of the string */
sum+=toindex(*s);
/* Increment the pointer of the string to point next character in the string */
s++;
/* Calculate the length of the string */
len++;
}
/* Finally print the sum of the numbers in the string */
printf("the sum of the digits of %s is %d",(s-len),sum);
return 0;
}