Check whether the Average Character of the String is present or not | CPP program to check if the average character is present in the string or not ; Checks if the character is present ; Get the length of string ; Iterate from i = 0 to the length of the string to check if the character is present in the string ; Finds the average character of the string ; Calculate the sum of ASCII values of each character ; Calculate average of ascii values ; Convert the ASCII value to character and return it ; Driver code ; Get the average character ; Check if the average character is present in string or not
#include <bits/stdc++.h> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool check_char ( char * st , char ch ) { int l = strlen ( st ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( st [ i ] == ch ) return true ; } return false ; } char find_avg ( char * st ) { int i , sm = 0 ; int l = strlen ( st ) ; char ch ; for ( i = 0 ; i < l ; i ++ ) { ch = st [ i ] ; sm = sm + ( int ) ( ch ) ; } int avg = ( int ) ( floor ( sm / l ) ) ; return ( ( char ) ( avg ) ) ; } int main ( ) { char st [ ] = " ag23sdfa " ; char ch = find_avg ( st ) ; cout << ch << endl ; if ( check_char ( st , ch ) == true ) cout << " Yes " ; else cout << " No " ; return 0 ; }