Processing a string Codechef Solution

Hello Programmers In this post, you will know how to solve the Processing a string Codechef Solution. The Problem Code: KOL15A

Processing a string Codechef Solution
Processing a string Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Ezoicreport this adProblem

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 Solutions 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 BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Prime Generator Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad