Saturday 11 November 2017

C Program to Read a String and find the Sum of all Digits in the String


Code:

#include
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);
}

Output:

Enter the string containing both digits and alphabet
hello100
NO. of Digits in the string = 3
Sum of all digits = 1



More C programs











100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...