112k views
0 votes
Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal.

User Crowlix
by
4.6k points

1 Answer

4 votes

A function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal.

Step-by-step explanation:

The function is ordinal_sum. The string can be accepted in the variable str " ".

#include <iostream>

#include <string>

#include <vector>

using namespace std;

long long int ordinal_sum (string str, vector<long long int>& sumArr)

{ int l = str.length();

int sum = 0;

long long int bigSum = 0L;

for (int i = 0; i < l; i++) {

if (str[i] == ' ') {

bigSum += sum;

sumArr.push_back(sum);

sum = 0;

}

else

sum += str[i];

}

sumArr.push_back(sum);

bigSum += sum;

return bigSum;

}

int main()

{

string str = " "

vector<long long int> sumArr;

long long int sum = ASCIIWordSum(str, sumArr);

cout << "Sum of ASCII values:" << std::endl;

for (auto x : sumArr)

cout << x << " ";

cout << endl << "Total sum -> " << sum;

return 0;

}

User Herzl Shemuelian
by
4.5k points