Here we will see how to count the average of the ASCII values of each character in a given string. Suppose the string is “ABC”. The asci values are 65, 66, 67. So the average of these three is 66.
Algorithm
asciiAverage(String)
Begin sum := 0 for each character c in String, do sum := sum + ASCII of c done return sum/length of String End
Example
#include<iostream> using namespace std; float asciiAverage(string str){ int sum = 0; for(int i = 0; i<str.size(); i++){ sum += int(str[i]); } return sum/str.size(); } main() { string str; cout << "Enter a string: "; cin >> str; cout << "ASCII average is: " << asciiAverage(str); }
Output
Enter a string: Hello ASCII average is: 100