Find the first maximum length even word from a string | Java program to find maximum length even word ; 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
class GFG { static String findMaxLenEven ( String str ) { int n = str . length ( ) ; int i = 0 ; int currlen = 0 ; int maxlen = 0 ; int st = - 1 ; while ( i < n ) { if ( str . charAt ( 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 " - 1" ; return str . substring ( st , st + maxlen ) ; } public static void main ( String args [ ] ) { String str = " this ▁ is ▁ a ▁ test ▁ string " ; System . out . println ( findMaxLenEven ( str ) ) ; } }