Sunday, 28 Nov 2021

# Find the single digit sum of alphabetical values of a string

Given string S of size N, the task is to find the single-digit sum by the repetitive sum of digits of the value obtained by the sum of order of all alphabets in the given string.The order of alphabets is given by the position at which they occur in English Alaphabets.Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Examples:Input: S = “geek”Output: 1Explanation:The value obtained by the sum order of alphabets is 7 + 5 + 5 + 11 = 28.The single digit sum obtained by sum of 28 = 2 + 8 = 10 = 1 + 0 = 1.Input: S = “GeeksforGeeks”Output: 7Approach: The given problem can be solved by first find the sum of orders of all the alphabets present in the given string S by adding the value (S[i] – ‘a’ + 1) or (S[i] – ‘A’ + 1) if S[i] is lowercase or uppercase character. After finding the value of sum, find the single digit of this value using the approach discussed in this article.Below is the implementation of the above approach:C++  #include using namespace std;  int findTheSum(string str){    string alpha;          for (int i = 0; i < str.length(); i++) {                  if ((str[i] >= ‘A’ && str[i] = ‘a’ && str[i] = ‘A’ && alpha[i] 0 || n > 9) {        if (score == 0) {            score = n;            n = 0;        }        n += score % 10;        score /= 10;    }          return n;}  int main(){    string S = “GeeksforGeeks”;    cout