The ASCII value of the ward is the integer presentation based on ASCII standards. In this problem, we are given a sentence and we have to calculate the sum of ASCII values of each word in the sentence.
For this we will have to find the ASCII values of all the characters of the sentence and then add them up, this will give the sum of ASCII values of letters in this word. we have to do the same for all words and finally, we will add all the sums and give a final sum of ASCII values of each word of the sentence.
for example
the sentence is “I love tutorials point”.
Output will be
105 438 999 554 2096
Example
#include <iostream> #include <string> #include <vector> using namespace std; long long int sumcalc (string str, vector < long long int >&arrsum) { int l = str.length (); int sum = 0; long long int bigSum = 0L; for (int i = 0; i < l; i++) { if (str[i] == ' ') { bigSum += sum; arrsum.push_back (sum); sum = 0; } else sum += str[i]; } arrsum.push_back (sum); bigSum += sum; return bigSum; } int main () { string str = "i love tutorials point"; vector < long long int >arrsum; cout<< "The string is "<<str<<endl; long long int sum = sumcalc (str, arrsum); cout << "Sum of ASCII values: "; for (auto x:arrsum) cout << x << " "; cout << endl << "Total sum -> " << sum; return 0; }
Output
The string is i love tutorials point Sum of ASCII values: 105 438 999 554 Total sum -> 2096