Processing a string Codechef Solution| Problem Code: KOL15A

Hello coders, today we are going to solve Processing a string Codechef Solution| Problem Code: KOL15A.

Processing a string Codechef Solution
Processing a string Codechef Solution

Problem

Given an alphanumeric string made up of digits and lower case Latin characters only, find the sum of all the digit characters in the string.

Input

  • The first line of the input contains an integer T denoting the number of test cases. Then T test cases follow.
  • Each test case is described with a single line containing a string S, the alphanumeric string.

Output

  • For each test case, output a single line containing the sum of all the digit characters in that string.

Constraints

  • 1 ≤ T ≤ 1000
  • 1 ≤ |S| ≤ 1000, where |S| is the length of the string S.

Example

Input:
1
ab1231da
Output:
7

Explanation

The digits in this string are 1, 2, 3 and 1. Hence, the sum of all of them is 7.

 Processing a string CodeChef Solution in CPP

#include <bits/stdc++.h>
using namespace std;
int  get_sum_digits(string word)
{
    // Case 1 : string is empty
    if(word.size() == 0)
        return 0;
    // Case 2: string is NOT empty
    int sum = 0;
    for(int i=0; i < word.size(); i++)
    {
        cout << "sum is: "<<sum<<endl;
        cout << "single char: "<<word[i]<<endl;
        if(isdigit(word[i]))
        {
            // 49: is the ASCI Value of 1
            sum += (int)word[i] - 48;
        }
    }
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int test_cases;
    cin >> test_cases;
    string word;
    while(test_cases--)
    {
        cin >> word;
        cout << get_sum_digits(word)<<endl;
    }
    return 0;
}

Disclaimer: The above Problem (Processing a string) is generated by CodeChef but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purpose.

Sharing Is Caring