Question #183023

Instructions

Write a program that prompts the user to enter a string. The program outputs the sum of the values (collating sequence or ASCII value) of the characters in the string. For example, if the string is "spring", then the sum of the values of the characters is 115 + 112 + 114 + 105 + 110 + 103 = 659.


Expert's answer

#include <iostream>
#include <string>

int main()
{
    std::cout << "Please enter a string: ";
    std::string str;
    std::getline(std::cin, str);

    int sum = 0;
    for(size_t i = 0; i < str.size(); ++i)
    {
        sum += str[i];
    }

    std::cout << "The sum of the values of the characters is " << sum << "\n";
    
    return 0;    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS