Find the character made by adding all the characters of the given string - GeeksforGeeks

06 Jul, 2021
Given a string str consisting of lowercase English alphabets. The task is to add all the character values i.e. ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, ..., ‘z’ = 26 and output the character corresponding to the sum value. If it exceeds 26 then take sum % 26.Examples:  
Input: str = “gfg” Output: t (g + f + g) = 7 + 6 + 7 = 20 and t = 20Input: str = “geeks” Output: u  
 
Approach:  
Find the sum of all character of the string and store it in a variable sum.If sum % 26 = 0 then print ‘z’.Else update sum = sum % 26 and print (sum + ‘a’ + 1).
Find the sum of all character of the string and store it in a variable sum.
If sum % 26 = 0 then print ‘z’.
Else update sum = sum % 26 and print (sum + ‘a’ + 1).
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the required characterchar getChar(string str){     // To store the sum of the characters    // of the given string    int sum = 0;     for (int i = 0; i < str.length(); i++) {         // Add the current character to the sum        sum += (str[i] - 'a' + 1);    }     // Return the required character    if (sum % 26 == 0)        return 'z';    else {        sum = sum % 26;        return (char)('a' + sum - 1);    }} // Driver codeint main(){    string str = "gfg";     cout << getChar(str);     return 0;}
// Java implementation of the approachclass GFG{     // Function to return the required characterstatic char getChar(String str){     // To store the sum of the characters    // of the given string    int sum = 0;     for (int i = 0; i < str.length(); i++)    {         // Add the current character to the sum        sum += (str.charAt(i) - 'a' + 1);    }     // Return the required character    if (sum % 26 == 0)        return 'z';    else    {        sum = sum % 26;        return (char)('a' + sum - 1);    }} // Driver codepublic static void main (String[] args){    String str = "gfg";     System.out.println(getChar(str));}} // This code is contributed by AnkitRai01
# Python3 implementation of the approach # Function to return the required characterdef getChar(strr):     # To store the summ of the characters    # of the given string    summ = 0     for i in range(len(strr)):         # Add the current character to the summ        summ += (ord(strr[i]) - ord('a') + 1)     # Return the required character    if (summ % 26 == 0):        return ord('z')    else:        summ = summ % 26        return chr(ord('a') + summ - 1) # Driver codestrr = "gfg" print(getChar(strr)) # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System; class GFG{     // Function to return the required characterstatic char getChar(String str){     // To store the sum of the characters    // of the given string    int sum = 0;     for (int i = 0; i < str.Length; i++)    {         // Add the current character to the sum        sum += (str[i] - 'a' + 1);    }     // Return the required character    if (sum % 26 == 0)        return 'z';    else    {        sum = sum % 26;        return (char)('a' + sum - 1);    }} // Driver codepublic static void Main (String[] args){    String str = "gfg";     Console.WriteLine(getChar(str));}}     // This code is contributed by PrinciRaj1992
<script>    // Javascript implementation of the approach         // Function to return the required character    function getChar(str)    {         // To store the sum of the characters        // of the given string        let sum = 0;         for (let i = 0; i < str.length; i++)        {             // Add the current character to the sum            sum += (str[i].charCodeAt() - 'a'.charCodeAt() + 1);        }         // Return the required character        if (sum % 26 == 0)            return 'z';        else {            sum = sum % 26;            return String.fromCharCode('a'.charCodeAt() + sum - 1);        }    }         let str = "gfg";    document.write(getChar(str));         // This code is contributed by divyeshrabadiya07.</script>
t
 
mohit kumar 29
ankthon
princiraj1992
divyeshrabadiya07
saurabh1990aror
gabaa406
ASCII
school-programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Naive algorithm for Pattern Searching
Vigenère Cipher
Hill Cipher
Count words in a given string
How to Append a Character to a String in C
Convert character array to string in C++
Program to count occurrence of a given character in a string
sprintf() in C
Converting Roman Numerals to Decimal lying between 1 to 3999