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.
#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;  Â
}
Comments
Leave a comment