Sunday, January 12, 2025
Google search engine
HomeData Modelling & AIFind the sum of alphabetical order of characters in a string

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

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 = "neveropen";
    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 = "neveropen";
    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 = "neveropen"
    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 = "neveropen";
 
    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 = "neveropen";
        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 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments