Answer to Question #183023 in C++ for Vincenzo Santoro

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.


1
Expert's answer
2021-05-01T14:36:53-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS