# 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 with the length of string i.e, total number of characters in the string.

## C++

 `// C++ code to find average  ` `// of ASCII characters ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find average  ` `// of ASCII value of chars ` `int` `averageValue(string s) ` `{ ` `    ``int` `sum_char = 0; ` ` `  `    ``// loop to sum the ascii ` `    ``// value of chars ` `    ``for` `(``int` `i = 0; i < s.length(); i++) ` `    ``{ ` `        ``sum_char += (``int``)s[i]; ` `    ``} ` ` `  `    ``// Returning average of chars ` `    ``return` `sum_char / s.length(); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"GeeksforGeeks"``; ` ` `  `    ``cout << averageValue(s); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed  ` `// by Subhadeep `

## Java

 `// Java code to find average of ASCII characters ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find average of ASCII value of chars ` `    ``public` `static` `int` `averageValue(String s) ` `    ``{ ` `        ``int` `sum_char = ``0``; ` ` `  `        ``// loop to sum the ascii value of chars ` `        ``for` `(``int` `i = ``0``; i < s.length(); i++) { ` `            ``sum_char += (``int``)s.charAt(i); ` `        ``} ` ` `  `        ``// Returning average of chars ` `        ``return` `sum_char / s.length(); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``String s = ``"GeeksforGeeks"``; ` ` `  `        ``System.out.println(averageValue(s)); ` `    ``} ` `} `

## Python 3

 `# Python 3 code to find average  ` `# of ASCII characters ` ` `  `# Function to find average  ` `# of ASCII value of chars ` `def` `averageValue(s): ` `    ``sum_char ``=` `0` ` `  `    ``# loop to sum the ascii ` `    ``# value of chars ` `    ``for` `i ``in` `range``(``len``(s)): ` `        ``sum_char ``+``=` `ord``(s[i]) ` ` `  `    ``# Returning average of chars ` `    ``return` `sum_char ``/``/` `len``(s) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``s ``=` `"GeeksforGeeks"` ` `  `    ``print``(averageValue(s)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# code to find average of  ` `// ASCII characters  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `// Function to find average of ` `// ASCII value of chars  ` `public` `static` `int` `averageValue(String s)  ` `{  ` `    ``int` `sum_char = 0;  ` ` `  `    ``// loop to sum the ascii value of chars  ` `    ``for` `(``int` `i = 0; i < s.Length; i++) ` `    ``{  ` `        ``sum_char += (``int``)s[i];  ` `    ``}  ` ` `  `    ``// Returning average of chars  ` `    ``return` `sum_char / s.Length;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `    ``String s = ``"GeeksforGeeks"``;  ` ` `  `    ``Console.Write(averageValue(s));  ` `}  ` `}  ` ` `  ` `  `// This code is contributed  ` `// by PrinciRaj1992 `

## PHP

 ` `

Output:

```101
```

Time Complexity: O(l), where l is the length of the string.
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.