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:
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; }
Output
133
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.