C Programming Code Examples C > Strings Code Examples Program to Read a String and find the Sum of all Digits in the String Program to Read a String and find the Sum of all Digits in the String This program takes a string containing both digits and alphabet as input and finds the sum of all digits in the string. 1. Take the string containing both digits and alphabet as input and store it in the array string[]. 2. Using for loop and if statement check for the digits in the array. If it is, then increment the variable nc by 1 and increment the variable sum with the current digit. 3. Do the step-2 till the end of the input string. 4. Variable nc gives the count of number of digits in the array and variable sum gives the sum of all the digits in the array. #include <stdio.h> void main() { char string[80]; int count, nc = 0, sum = 0; printf("Enter the string containing both digits and alphabet \n"); scanf("%s", string); 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); }