Skip to content
Related Articles

Related Articles

Find the sum of alphabetical order of characters in a string

Improve Article
Save Article
  • Last Updated : 04 Jan, 2022
Improve Article
Save Article

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 = “GeeksforGeeks”

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 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>


Output

133

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!