Dada la string S de tamaño N , la tarea es encontrar la suma del valor alfabético de cada carácter en la string dada.
Ejemplos:
Entrada: S = «geek»
Salida: 28
Explicación:
El valor obtenido por el orden de la suma de letras es 7 + 5 + 5 + 11 = 28.Entrada: S = «GeeksforGeeks»
Salida: 133
Acercarse:
- Recorre todos los caracteres presentes en la string dada S.
- Para cada carácter en minúscula, agregue el valor (S[i] – ‘a’ + 1) a la respuesta final, o si es un carácter en mayúscula, agregue (S[i] – ‘A’ + 1) a la respuesta final .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; int findTheSum(string alpha) { // Stores the sum of order of values int score = 0; for (int i = 0; i < alpha.length(); i++) { // Find the score if (alpha[i] >= 'A' && alpha[i] <= 'Z') score += alpha[i] - 'A' + 1; else score += alpha[i] - 'a' + 1; } // Return the resultant sum return score; } // Driver Code int main() { string S = "GeeksforGeeks"; cout << findTheSum(S); return 0; }
Java
// Java code to implement the above approach import java.util.*; public class GFG { static int findTheSum(String alpha) { // Stores the sum of order of values int score = 0; for (int i = 0; i < alpha.length(); i++) { // Find the score if (alpha.charAt(i) >= 'A' && alpha.charAt(i) <= 'Z') score += alpha.charAt(i) - 'A' + 1; else score += alpha.charAt(i) - 'a' + 1; } // Return the resultant sum return score; } // Driver code public static void main(String args[]) { String S = "GeeksforGeeks"; System.out.println(findTheSum(S)); } } // This code is contributed by Samim Hossain Mondal.
Python3
# Python3 program for the above approach def findTheSum(alpha): # Stores the sum of order of values score = 0 for i in range(0, len(alpha)): # Find the score if (ord(alpha[i]) >= ord('A') and ord(alpha[i]) <= ord('Z')): score += ord(alpha[i]) - ord('A') + 1 else: score += ord(alpha[i]) - ord('a') + 1 # Return the resultant sum return score # Driver Code if __name__ == "__main__": S = "GeeksforGeeks" print(findTheSum(S)) # This code is contributed by rakeshsahni
C#
// C# code to implement the above approach using System; class GFG { static int findTheSum(string alpha) { // Stores the sum of order of values int score = 0; for (int i = 0; i < alpha.Length; i++) { // Find the score if (alpha[i] >= 'A' && alpha[i] <= 'Z') score += alpha[i] - 'A' + 1; else score += alpha[i] - 'a' + 1; } // Return the resultant sum return score; } // Driver code public static void Main() { string S = "GeeksforGeeks"; Console.Write(findTheSum(S)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach function findTheSum(alpha) { // Stores the sum of order of values let score = 0; for (let i = 0; i < alpha.length; i++) { // Find the score if (alpha[i].charCodeAt(0) >= 'A'.charCodeAt(0) && alpha[i].charCodeAt(0) <= 'Z'.charCodeAt(0)) score += alpha[i].charCodeAt(0) - 'A'.charCodeAt(0) + 1; else score += alpha[i].charCodeAt(0) - 'a'.charCodeAt(0) + 1; } // Return the resultant sum return score; } // Driver Code let S = "GeeksforGeeks"; document.write(findTheSum(S)); // This code is contributed by Potta Lokesh </script>
Producción
133