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 readonly string ; Driver Code ; Function Call
using System ; using System . Text ; class GFG { public static String generatePassword ( String [ ] s , char T ) { StringBuilder result = new StringBuilder ( ) ; foreach ( String currentString in s ) { String [ ] person = currentString . Split ( ' : ' ) ; String name = person [ 0 ] ; String number = person [ 1 ] ; int n = name . Length ; int max = 0 ; for ( int i = 0 ; i < number . Length ; i ++ ) { int temp = Int32 . Parse ( String . Join ( " " , number [ i ] ) ) ; if ( temp > max && temp <= n ) max = temp ; } if ( max == 0 ) result . Append ( T ) ; else result . Append ( String . Join ( " " , name [ max - 1 ] ) ) ; } return result . ToString ( ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { " Geeks : 89167" , " gfg : 68795" } ; char T = ' X ' ; Console . WriteLine ( generatePassword ( arr , T ) ) ; } }