Find the first maximum length even word from a string | Function to find maximum length even word ; To store length of current word . ; To store length of maximum length word . ; To store starting index of maximum length word . ; If current character is space then word has ended . Check if it is even length word or not . If yes then compare length with maximum length found so far . ; Set currlen to zero for next word . ; Update length of current word . ; Check length of last word . ; If no even length word is present then return - 1. ; Driver code
function findMaxLenEven ( str ) { var n = str . length ; var i = 0 ; var currlen = 0 ; var maxlen = 0 ; var st = - 1 ; while ( i < n ) { if ( str [ i ] == ' ' ) { if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } currlen = 0 ; } else { currlen ++ ; } i ++ ; } if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } if ( st == - 1 ) return " " ; return str . substr ( st , maxlen ) ; } var str = " " ; document . write ( findMaxLenEven ( str ) ) ;