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.


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.


  • 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.


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


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




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;
            // 49: is the ASCI Value of 1
            sum += (int)word[i] - 48;
    return sum;
int main()
    int test_cases;
    cin >> test_cases;
    string word;
        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

Leave a Reply

Your email address will not be published. Required fields are marked *