Average of ASCII values of characters of a given string?



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
Updated on: 2020-07-02T07:26:14+05:30

883 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements