Generate a string from an array of alphanumeric strings based on given conditions | C ++ program for the above approach ; Function to generate required string ; To store the result ; Split name and number ; Stores the maximum number less than or equal to the length of name ; Check for number by parsing it to integer if it is greater than max number so far ; Check if no such number is found then we append X to the result . ; Otherwise ; Append max index of the name ; Return the final string ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string generatePassword ( vector < string > arr , char T ) { string result ; for ( auto s : arr ) { int index ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' : ' ) { index = i ; break ; } } string name = s . substr ( 0 , index ) ; string number = s . substr ( index + 1 , s . size ( ) - index - 1 ) ; int n = name . length ( ) ; int max = 0 ; for ( int i = 0 ; i < number . length ( ) ; i ++ ) { int temp = number [ i ] - '0' ; if ( temp > max && temp <= n ) max = temp ; } if ( max == 0 ) result . push_back ( T ) ; else result . push_back ( name [ max - 1 ] ) ; } return result ; } int main ( ) { vector < string > arr = { " Geeks : 89167" , " gfg : 68795" } ; char T = ' X ' ; cout << ( generatePassword ( arr , T ) ) ; }