Encuentre el carácter creado sumando todos los caracteres de la string dada

Dada una string str que consta de alfabetos ingleses en minúsculas. La tarea es sumar todos los valores de los caracteres, es decir , ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, …, ‘z’ = 26 y generar el carácter correspondiente al valor de la suma. Si excede 26, tome la suma % 26.
Ejemplos: 
 

Entrada: str = “gfg” 
Salida:
(g + f + g) = 7 + 6 + 7 = 20 y t = 20
Entrada: str = “geeks” 
Salida:
 

Acercarse: 
 

  1. Encuentra la suma de todos los caracteres de la string y guárdala en una variable sum .
  2. Si suma% 26 = 0 , imprima ‘z’ .
  3. De lo contrario, actualice sum = sum % 26 e imprima (sum + ‘a’ + 1) .

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

<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>
Producción: 

t

 

Publicación traducida automáticamente

Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *