# Average of ASCII values of characters of a given string?

CServer Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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