Given a string of alphanumeric characters, the task is to check whether the average character of the string is present or not. Average character refers to the character corresponding to the ASCII value which is the floor of the average value of the ASCII values of all characters in the string.
Examples:
Input: abcdef
Output: d
Yes
Explanation:
string = "abcdef"
ASCII values of a = 97, b=98,
c=99, d=100, e=101, f=101
Sum of these values is 597
Average is 99.5 ~ 100
Character of ASCII value 100 = d
Hence d is present in the string.
Input: MNFGH
Output: J
No
Approach: The approach to solve this problem is very simple. It can be solved in the following steps:
- Find the sum of ASCII values of all the characters from the given string.
- Find the average of the ASCII value as average = (sum / numberOfCharacters)
- Get the character of this average ASCII value. Print this character
- Check if this character is present in the string or not. Print Yes or No accordingly.
Below is the implementation of the above approach:
Implementation:
C++
// CPP program to check if the average character // is present in the string or not #include <bits/stdc++.h> #include <math.h> using namespace std; // Checks if the character is present bool check_char(char* st, char ch) { // Get the length of string int l = strlen(st); // Iterate from i=0 to // the length of the string // to check if the character // is present in the string for (int i = 0; i < l; i++) { if (st[i] == ch) return true; } return false; } // Finds the average character of the string char find_avg(char* st) { int i, sm = 0; int l = strlen(st); char ch; for (i = 0; i < l; i++) { ch = st[i]; // Calculate the sum of ASCII // values of each character sm = sm + (int)(ch); } // Calculate average of ascii values int avg = (int)(floor(sm / l)); // Convert the ASCII value to character // and return it return ((char)(avg)); } // Driver code int main() { char st[] = "ag23sdfa"; // Get the average character char ch = find_avg(st); cout << ch << endl; // Check if the average character // is present in string or not if (check_char(st, ch) == true) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java program to check if the average character // is present in the string or not import java.math.*; class GFG { // Checks if the character is present static boolean check_char(String st, char ch) { // Get the length of string int l = st.length(); // Iterate from i=0 to // the length of the string // to check if the character // is present in the string for (int i = 0; i < l; i++) { if (st.charAt(i) == ch) return true; } return false; } // Finds the average character of the string static char find_avg(String st) { int i, sm = 0; int l = st.length(); char ch; for (i = 0; i < l; i++) { ch = st.charAt(i); // Calculate the sum of ASCII // values of each character sm = sm + (int)(ch); } // Calculate the average of ASCII values int avg = (int)(Math.floor(sm / l)); // Convert the ASCII value to character // and return it return ((char)(avg)); } // Driver code public static void main(String[] args) { String st = "ag23sdfa"; // Get the average character char ch = find_avg(st); System.out.println(ch); // Check if the average character // is present in string or not if (check_char(st, ch) == true) System.out.println("Yes"); else System.out.println("No"); } } |
Python3
# Python 3 program to check if the average # character is present in the string or not from math import floor # Checks if the character is present def check_char(st, ch): # Get the length of string l = len(st) # Iterate from i=0 to # the length of the string # to check if the character # is present in the string for i in range(l): if (st[i] == ch): return True return False # Finds the average character # of the string def find_avg(st): sm = 0 l = len(st) for i in range(l): ch = st[i] # Calculate the sum of ASCII # values of each character sm = sm + ord(ch) # Calculate average of ascii values avg = int(floor(sm / l)) # Convert the ASCII value to character # and return it return (chr(avg)) # Driver code if __name__ == '__main__': st = "ag23sdfa" # Get the average character ch = find_avg(st) print(ch) # Check if the average character # is present in string or not if (check_char(st, ch) == True): print("Yes") else: print("No") # This code is contributed by # Surendra_Gangwar |
C#
// C# program to check if the average character // is present in the string or not using System; class GFG { // Checks if the character is present static bool check_char(string st, char ch) { // Get the length of string int l = st.Length; // Iterate from i=0 to // the length of the string // to check if the character // is present in the string for (int i = 0; i < l; i++) { if (st[i] == ch) return true; } return false; } // Finds the average character of the string static char find_avg(string st) { int i, sm = 0; int l = st.Length; char ch; for (i = 0; i < l; i++) { ch = st[i]; // Calculate the sum of ASCII // values of each character sm = sm + (int)(ch); } // Calculate the average of ASCII values int avg = (int)(Math.Floor((double)(sm / l))); // Convert the ASCII value to character // and return it return ((char)(avg)); } // Driver code public static void Main() { string st = "ag23sdfa"; // Get the average character char ch = find_avg(st); Console.WriteLine(ch); // Check if the average character // is present in string or not if (check_char(st, ch) == true) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Akanksha Rai |
Y No
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.
Recommended Posts:
- Check whether the given character is in upper case, lower case or non alphabetic character
- Find the character in first string that is present at minimum index in second string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Most frequent word in first String which is not present in second String
- Find repeated character present first in a string
- Find the largest Alphabetic character present in the string
- Check whether a given string is Heterogram or not
- Check whether count of distinct characters in a string is Prime or not
- Check whether the frequencies of all the characters in a string are prime or not
- C++ program to check whether a String is a Pangram or not
- Check whether the vowels in a string are in alphabetical order or not
- Check whether the binary equivalent of a number ends with given string or not
- Check if a String contains Anagrams of length K which does not contain the character X
- Check input character is alphabet, digit or special character
- Check if the given string of words can be formed from words present in the dictionary
- Python | Check if a Substring is Present in a Given String
- Check if given words are present in a string
- Check if binary representations of 0 to N are present as substrings in given binary string
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
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.

