Sum of the alphabetical values of the characters of a string | C ++ implementation of the approach ; Function to find string score ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ; } int main ( ) { string str [ ] = { " sahil " , " shashanak " , " sanjit " , " abhinav " , " mohit " } ; string s = " abhinav " ; int n = sizeof ( str ) / sizeof ( str [ 0 ] ) ; int score = strScore ( str , s , n ) ; cout << score << endl ; return 0 ; }