Find the sum of alphabetical order of characters in a string

Given string S of size N, the task is to find the sum of the alphabet value of each character in the given string.
Examples:
Input: S = “geek”
Output: 28
Explanation:
The value obtained by the sum order of alphabets is 7 + 5 + 5 + 11 = 28.Input: S = “zambiatek”
Output: 133
Approach:
- Traverse all the characters present in the given string S.
- For each lowercase character, add the value (S[i] – ‘a’ + 1) to the final answer, or if it is an uppercase character, add (S[i] – ‘A’ + 1) to the final answer.
Below is the implementation of the above approach:
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 Codeint main(){ string S = "zambiatek"; cout << findTheSum(S); return 0;} |
Java
// Java code to implement the above approachimport 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 codepublic static void main(String args[]){ String S = "zambiatek"; System.out.println(findTheSum(S));}}// This code is contributed by Samim Hossain Mondal. |
Python3
# Python3 program for the above approachdef 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 Codeif __name__ == "__main__": S = "zambiatek" print(findTheSum(S))# This code is contributed by rakeshsahni |
C#
// C# code to implement the above approachusing 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 = "zambiatek"; 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 = "zambiatek"; document.write(findTheSum(S)); // This code is contributed by Potta Lokesh </script> |
Output
133
Time Complexity: O(n)
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



