Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
Advertisements