Sum of Digits in a String

Finding Sum of Digits in a String

In this post we will be discuss the Program to find the sum of the digits in a given string i.e., within a given string we have to calculate and perform the addition operation on the digits that are present in the string. Also, the required logic is discussed and the source code is written in C++ which is compiled successfully to provide the best results.

Sum of Digits in a String

Problem Description

In XYZ country there is rule that car’s engine no. depends upon car’ number plate. Engine no is sum of all the integers present on car’s Number plate. The issuing authority has hired you in order to provide engine no. to the cars. Your task is to develop an algorithm which takes input as in form of string(Number plate) and gives back Engine number.

Input Description:
You are given a string ’s’

Output Description:
Print the engine number

Sample Input :
HR05-AA-2669

Sample Output :
28

Sample Input :
The1Coding2Bro3

Sample Output :
6

Explaination :

In this we will be discussing the Program to find the sum of the digits in a given string i.e., within a given string we have to calculate and perform the addition operation on the digits that are present in the string. This can be done through using the simple looping statements. We will run the loop till the null character and try to find the digit at every index of the string. If the digit is found at particular index, then sum it using the loops and addition operator. The same is followed till all the digits are summed up to provide the final summation value.

Program to find the sum of the digits in a given string

Logic to follow to come-up with the solution :

1. Declare the required sets of variables to use in the code.

2. Initialize a sum and count variable to zero and also declare the size of the string.

3. Now apply the for-loop form starting index till the null character of the string is found.

4. Now at particular index if the value or digit is greater than or equal to zero and smaller than or equal to nine.

5. Then increment the count variable and sum that particular value using the addition operator and store the value in the variable sum.

6. This same thing is followed until the loop is functioning i.e., till the null character of the string is found.

7. At last finally print the value of sum of digits in a string after coming out of the loop.

Let’s write the required code for the problem :

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main() {
    int n=0,sum=0;
    char str[100];
    cin>>str;
    for(int i=0;str[i]!='\0';i++)
    {
        if((str[i]>='0') && str[i]<='9')
        {
            n++;
            sum=sum+(str[i]-'0');
        }
    }
    cout<<sum;
    return 0;
}

Sample Test Cases have been passed successfully

Sum of Digits

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

Next Post Previous Post
No Comment
Add Comment
comment url

Best Buy Products