# Sum of the alphabetical values of the characters of a string

You are given an array of strings str, the task is to find the score of a given string s from the array. The score of a string is defined as the product of the sum of its characters’s alphabetical values with the position of the string in the array.

Examples:

Input: str[] = {“sahil”, “shashanak”, “sanjit”, “abhinav”, “mohit”}, s = “abhinav”
Output: 228
Sum of alphabetical values of “abhinav” = 1 + 2 + 8 + 9 + 14 + 1 + 22 = 57
Position of “abhinav” in str is 4, 57 x 4 = 228

Input: str[] = {“geeksforgeeks”, “algorithms”, “stack”}, s = “algorithms”
Output: 244

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Find the given string in the array and store the position of the string.
• Then calculate the sum of the alphabetical values of the given string.
• Multiply the position of the string in the given array with the value calculated in the previous step and print the result.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find string score ` `int` `strScore(string str[], string s, ``int` `n) ` `{ ` `    ``int` `score = 0, index; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(str[i] == s) { ` `            ``for` `(``int` `j = 0; j < s.length(); j++) ` `                ``score += s[j] - ``'a'` `+ 1; ` `            ``index = i + 1; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``score = score * index; ` `    ``return` `score; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str[] = { ``"sahil"``, ``"shashanak"` `                      ``, ``"sanjit"``, ``"abhinav"``, ``"mohit"` `}; ` `    ``string s = ``"abhinav"``; ` `    ``int` `n = ``sizeof``(str) / ``sizeof``(str); ` `    ``int` `score = strScore(str, s, n); ` `    ``cout << score << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `//  Java implementation of the approach ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  ` `  `// Function to find string score ` `static` `int` `strScore(String str[], String s, ``int` `n) ` `{ ` `    ``int` `score = ``0``, index=``0``; ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `        ``if` `(str[i] == s) { ` `            ``for` `(``int` `j = ``0``; j < s.length(); j++) ` `                ``score += s.charAt(j) - ``'a'` `+ ``1``; ` `            ``index = i + ``1``; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``score = score * index; ` `    ``return` `score; ` `} ` ` `  `// Driver code ` ` `  ` `  `    ``public` `static` `void` `main (String[] args) { ` `            ``String str[] = { ``"sahil"``, ``"shashanak"` `                    ``, ``"sanjit"``, ``"abhinav"``, ``"mohit"` `}; ` `    ``String s = ``"abhinav"``; ` `    ``int` `n = str.length; ` `    ``int` `score = strScore(str, s, n); ` `    ``System.out.println( score); ` `    ``} ` `} ` `// This code is contributed by anuj_67.. `

## Python3

 `# Python3 implementation of the approach  ` `# Function to find string score  ` `def` `strScore(``str``, s, n):  ` `    ``score ``=` `0` `    ``index ``=` `0` `    ``for` `i ``in` `range``(n):  ` `        ``if` `(``str``[i] ``=``=` `s): ` `            ``for` `j ``in` `range``(``len``(s)): ` `                ``score ``+``=` `(``ord``(s[j]) ``-`  `                          ``ord``(``'a'``) ``+` `1``) ` `            ``index ``=` `i ``+` `1` `            ``break` `    ``score ``=` `score ``*` `index ` `    ``return` `score ` ` `  `# Driver code  ` `str` `=` `[``"sahil"``, ``"shashanak"``, ``"sanjit"``,  ` `                  ``"abhinav"``, ``"mohit"` `]  ` `s ``=` `"abhinav"` `n ``=` `len``(``str``) ` `score ``=` `strScore(``str``, s, n);  ` `print``(score) ` ` `  `# This code is contributed  ` `# by sahishelangia `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `// Function to find string score  ` `static` `int` `strScore(String []str,  ` `                    ``String s, ``int` `n)  ` `{  ` `    ``int` `score = 0, index = 0;  ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{  ` `        ``if` `(str[i] == s)  ` `        ``{  ` `            ``for` `(``int` `j = 0; j < s.Length; j++)  ` `                ``score += s[j] - ``'a'` `+ 1;  ` `            ``index = i + 1;  ` `            ``break``;  ` `        ``}  ` `    ``}  ` ` `  `    ``score = score * index;  ` `    ``return` `score;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main (String[] args)  ` `{  ` `    ``String []str = { ``"sahil"``, ``"shashanak"``, ``"sanjit"``, ` `                     ``"abhinav"``, ``"mohit"` `};  ` `    ``String s = ``"abhinav"``;  ` `    ``int` `n = str.Length;  ` `    ``int` `score = strScore(str, s, n);  ` `    ``Console.Write( score);  ` `}  ` `}  ` ` `  `// This code is contributed by 29AjayKumar `

## PHP

 ` `

Output:

```228
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.