Average of ASCII values of characters of a given string
Given a string, the task is to find the average of ASCII values of characters in the string.
Examples:
Input: str = “for”
Output: 109
‘f’= 102, ‘o’ = 111, ‘r’ = 114
(102 + 111 + 114)/3 = 109
Input: str = “geeks”
Output: 105
Source: Microsoft Internship Experience
Approach: Start iterating through characters of the string and add their ASCII value to a variable. Finally, divide this sum of ASCII values of characters by the length of the string i.e., the total number of characters in the string.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int averageValue(string s)
{
int sum_char = 0;
for ( int i = 0; i < s.length(); i++)
{
sum_char += ( int )s[i];
}
return sum_char / s.length();
}
int main()
{
string s = "GeeksforGeeks" ;
cout << averageValue(s);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int averageValue(String s)
{
int sum_char = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
sum_char += ( int )s.charAt(i);
}
return sum_char / s.length();
}
public static void main(String[] args)
{
String s = "GeeksforGeeks" ;
System.out.println(averageValue(s));
}
}
|
Python 3
def averageValue(s):
sum_char = 0
for i in range ( len (s)):
sum_char + = ord (s[i])
return sum_char / / len (s)
if __name__ = = "__main__" :
s = "GeeksforGeeks"
print (averageValue(s))
|
C#
using System;
class GFG
{
public static int averageValue(String s)
{
int sum_char = 0;
for ( int i = 0; i < s.Length; i++)
{
sum_char += ( int )s[i];
}
return sum_char / s.Length;
}
public static void Main()
{
String s = "GeeksforGeeks" ;
Console.Write(averageValue(s));
}
}
|
Javascript
<script>
function averageValue(s)
{
let sum_char = 0;
for (let i = 0; i < s.length; i++) {
sum_char +=(s[i]).charCodeAt(0);
}
return Math.floor(sum_char / s.length);
}
let s = "GeeksforGeeks" ;
document.write(averageValue(s));
</script>
|
PHP
<?php
function averageValue( $s )
{
$sum_char = 0;
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
$sum_char += ord( $s [ $i ]);
}
return (int)( $sum_char / strlen ( $s ));
}
$s = "GeeksforGeeks" ;
echo averageValue( $s );
?>
|
complexity Analysis:
- Time Complexity: O(l), where l is the length of the string.
- Auxiliary Space: O(1)
Approach:
- Initialize a variable sum to 0 to store the sum of ASCII value of all characters.
- Traverse the string and for each character, add its ASCII value to sum.
- Calculate the average by dividing sum by the length of the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int averageValue(string s)
{
int sum = accumulate(s.begin(), s.end(), 0, []( int acc, char c)
{ return acc + ( int )c; });
return sum / s.length();
}
int main()
{
string s = "GeeksforGeeks" ;
cout << averageValue(s) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int averageValue(String s)
{
int sum = s.chars().sum();
return sum / s.length();
}
public static void main(String[] args){
String s = "GeeksforGeeks" ;
System.out.println(averageValue(s));
}
}
|
Python3
def averageValue(s):
sum = 0
for c in s:
sum + = ord (c)
return sum / / len (s)
s = "GeeksforGeeks"
print (averageValue(s))
|
C#
using System;
public class Program
{
public static int AverageValue( string s)
{
int sum = 0;
foreach ( char c in s)
{
sum += ( int )c;
}
return sum / s.Length;
}
public static void Main( string [] args)
{
string s = "GeeksforGeeks" ;
int average = AverageValue(s);
Console.WriteLine(average);
}
}
|
Javascript
function averageValue(s) {
let sum = s.split( '' ).reduce((acc, c) => acc + c.charCodeAt(0), 0);
return Math.floor(sum / s.length);
}
let s = "GeeksforGeeks" ;
console.log(averageValue(s));
|
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1)
Last Updated :
08 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...