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
def findMaxLenEven ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE i = 0 NEW_LINE currlen = 0 NEW_LINE maxlen = 0 NEW_LINE st = - 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( str [ i ] == ' ▁ ' ) : NEW_LINE INDENT if ( currlen % 2 == 0 ) : NEW_LINE INDENT if ( maxlen < currlen ) : NEW_LINE INDENT maxlen = currlen NEW_LINE st = i - currlen NEW_LINE DEDENT DEDENT currlen = 0 NEW_LINE DEDENT else : NEW_LINE INDENT currlen += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( currlen % 2 == 0 ) : NEW_LINE INDENT if ( maxlen < currlen ) : NEW_LINE INDENT maxlen = currlen NEW_LINE st = i - currlen NEW_LINE DEDENT DEDENT if ( st == - 1 ) : NEW_LINE INDENT print ( " trie " ) NEW_LINE return " - 1" NEW_LINE DEDENT return str [ st : st + maxlen ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " this ▁ is ▁ a ▁ test ▁ string " NEW_LINE print ( findMaxLenEven ( str ) ) NEW_LINE DEDENT