This C Program reads string and find the sum of all digits in the string. The program accepts a character string, then add all the character’s integer value, thereby summing all the digits of a string.
Here is source code of the C program to read a string and find the sum of all digits in the string.The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to find the sum of all digits present in the string
int count, nc = 0, sum = 0;
printf("Enter the string containing both digits and alphabet \n");
for (count = 0; string[count] != '\0'; count++)
if ((string[count] >= '0') && (string[count] <= '9'))
nc += 1;
sum += (string[count] - '0');
printf("NO. of Digits in the string = %d\n", nc);
printf("Sum of all digits = %d\n", sum);
$ cc pgm80.c
Enter the string containing both digits and alphabet
NO. of Digits in the string = 3
Sum of all digits = 1