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.
4. Exit.
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<iostream>
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;
}
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.
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Programming MCQs
- Apply for C++ Internship
- Apply for Information Technology Internship
- Apply for Computer Science Internship
- Buy C++ Books