Open in App
Not now

# Find the character made by adding all the characters of the given string

• Difficulty Level : Medium
• Last Updated : 22 Dec, 2022

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:
(g + f + g) = 7 + 6 + 7 = 20 and t = 20

Input: str = “geeks”
Output:

Approach:

1. Find the sum of all characters of the string and store it in a variable sum.
2. If sum % 26 = 0 then print ‘z’.
3. Else update sum = sum % 26 and print (sum + ‘a’ + 1).

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach` `#include ` `using` `namespace` `std;`   `// Function to return the required character` `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 code` `int` `main()` `{` `    ``string str = ``"gfg"``;`   `    ``cout << getChar(str);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the approach ` `class` `GFG` `{` `    `  `// Function to return the required character ` `static` `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 code ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``String str = ``"gfg"``; `   `    ``System.out.println(getChar(str)); ` `} ` `}`   `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 implementation of the approach`   `# Function to return the required character` `def` `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 code` `strr ``=` `"gfg"`   `print``(getChar(strr))`   `# This code is contributed by Mohit Kumar`

## C#

 `// C# implementation of the approach ` `using` `System;`   `class` `GFG` `{` `    `  `// Function to return the required character ` `static` `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 code ` `public` `static` `void` `Main (String[] args) ` `{ ` `    ``String str = ``"gfg"``; `   `    ``Console.WriteLine(getChar(str)); ` `} ` `}` `    `  `// This code is contributed by PrinciRaj1992`

## Javascript

 ``

Output:

`t`

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

My Personal Notes arrow_drop_up
Related Articles