This is a C++ Program to Find the Sum of ASCII Value of all Elements in a String.
The program takes a string and prints the sum ASCII values of all the characters.
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.
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.
using namespace std;
int main ()
int i, sum = 0;
cout << "Enter a string : ";
for (i = 0; str[i] != '\0'; i++)
sum = sum + str[i];
cout << "Sum of all characters : " << sum;
1. The user is asked to enter a string. It is stored in ‘str’.
2. A variable ‘sum’ is initialized as 0.
3. Using a for loop, ASCII value of every character in the string is added to sum.
4. The total sum of the characters is then printed.
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
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.