Average of ASCII values of characters of a given string?

CServer Side ProgrammingProgramming

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

Live Demo

#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
Published on 26-Jul-2019 14:47:51