This is a C++ Program to Find the Sum of ASCII Value of all Elements in a String.
Problem Description
The program takes a string and prints the sum ASCII values of all the characters.
Problem Solution
1. The program takes a string.
2. Using a for loop, the ASCII equivalent of each character is added.
3. The resultant sum is printed.
4. Exit.
C++ Program/Source code
Here is the source code of C++ Program to Find the Sum of ASCII Value of all Elements in a String. The program output is shown below.
#include
using namespace std;
int main ()
{
char str[50];
int i, sum = 0;
cout << "Enter a string : ";
gets(str);
for (i = 0; str[i] != '\0'; i++)
sum = sum + str[i];
cout << "Sum of all characters : " << sum;
return 0;
}
Runtime Test Cases
Case 1 :
Enter a string : found
Sum of all characters : 540
Case 2 :
Enter a string : 24
Sum of all characters : 102
Case 3 :
Enter a string : a
Sum of all characters : 97