Modify array by removing characters from their Hexadecimal representations which are present in a given string | C ++ program for the above approach ; Function to convert a decimal number to its equivalent hexadecimal number ; Function to convert hexadecimal number to its equavalent decimal number ; Stores characters with their respective hexadecimal values ; Stores answer ; Traverse the string ; If digit ; If character ; Return the answer ; Function to move all the alphabets to front ; Function to modify each array element by removing characters from their hexadecimal representation which are present in a given string ; Traverse the array ; Stores hexadecimal value ; Remove the characters from hexadecimal representation present in string S ; Stores decimal value ; Replace array element ; Print the modified array ; Driven Program ; Given array ; Given string ; Function call to modify array by given operations
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decHex ( int n ) { char alpha [ ] = { ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' } ; string ans ; while ( n > 0 ) { if ( n % 16 < 10 ) { ans += to_string ( n % 16 ) ; } else { ans += alpha [ n % 16 - 10 ] ; } n /= 16 ; } reverse ( ans . begin ( ) , ans . end ( ) ) ; return ans ; } int hexDec ( string convertedHex ) { char mp [ ] = { 10 , 11 , 12 , 13 , 14 , 15 } ; int ans = 0 ; int pos = 0 ; reverse ( convertedHex . begin ( ) , convertedHex . end ( ) ) ; for ( char ch : convertedHex ) { if ( isdigit ( ch ) ) { ans += ( ( int ) pow ( 16 , pos ) ) * ( ch - '0' ) ; } else { ans += ( ( int ) pow ( 16 , pos ) ) * mp [ ch - ' A ' ] ; } pos += 1 ; } return ans ; } string removeChars ( string hexaVal , string S ) { set < char > setk ; for ( char ch : S ) { setk . insert ( ch ) ; } string ans = " " ; for ( char ch : hexaVal ) { if ( setk . find ( ch ) != setk . end ( ) ) { continue ; } ans += ch ; } return ans ; } void convertArr ( int arr [ ] , int N , string S ) { for ( int i = 0 ; i < N ; i ++ ) { string hexaVal = decHex ( arr [ i ] ) ; string convertedHex = removeChars ( hexaVal , S ) ; int decVal = hexDec ( convertedHex ) ; arr [ i ] = decVal ; } for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 74 , 91 , 31 , 122 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string S = "1AB " ; convertArr ( arr , N , S ) ; return 0 ; }