Find the first maximum length even word from a string

21 Apr, 2021
Given a string of words separated by spaces. The task is to find the first maximum length even word from the string. Eg: “You are given an array of n numbers” The answer would be “an” and not “of” because “an” comes before “of”.Examples:  
Input:  "this is a test string"
Output:  string
Even length words are this, is, test, string. Even
maximum length word is string.

Input:  "geeksforgeeks is a platform for geeks"
Output:  platform
Only even length word is platform.
 
Approach: The idea is to traverse the input string and find length of each word. Check if the length of word is even or not. If even, then compare length with maximum length found so far. If length is strictly greater than maximum length then store current word as required string.Below is the implementation of above approach:  
C++
Java
Python 3
C#
Javascript
// C++ program to find maximum length even word #include <bits/stdc++.h>using namespace std; // Function to find maximum length even wordstring findMaxLenEven(string str){    int n = str.length();     int i = 0;     // To store length of current word.    int currlen = 0;     // To store length of maximum length word.    int maxlen = 0;     // To store starting index of maximum    // length word.    int st = -1;     while (i < n) {         // 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.        if (str[i] == ' ') {            if (currlen % 2 == 0) {                if (maxlen < currlen) {                    maxlen = currlen;                    st = i - currlen;                }            }             // Set currlen to zero for next word.            currlen = 0;        }        else {            // Update length of current word.            currlen++;        }         i++;    }     // Check length of last word.    if (currlen % 2 == 0) {        if (maxlen < currlen) {            maxlen = currlen;            st = i - currlen;        }    }     // If no even length word is present    // then return -1.    if (st == -1)        return "-1";     return str.substr(st, maxlen);} // Driver codeint main(){    string str = "this is a test string";     cout << findMaxLenEven(str);     return 0;}
// Java program to find maximum length even wordclass GFG{     // Function to find maximum length even wordstatic String findMaxLenEven(String str){    int n = str.length();     int i = 0;     // To store length of current word.    int currlen = 0;     // To store length of maximum length word.    int maxlen = 0;     // To store starting index of maximum    // length word.    int st = -1;     while (i < n)    {         // 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.        if (str.charAt(i) == ' ')        {            if (currlen % 2 == 0)            {                if (maxlen < currlen)                {                    maxlen = currlen;                    st = i - currlen;                }            }             // Set currlen to zero for next word.            currlen = 0;        }        else        {            // Update length of current word.            currlen++;        }         i++;    }     // Check length of last word.    if (currlen % 2 == 0)    {        if (maxlen < currlen)        {            maxlen = currlen;            st = i - currlen;        }    }     // If no even length word is present    // then return -1.    if (st == -1)        return "-1";     return str.substring(st, st + maxlen);} // Driver codepublic static void main(String args[]){    String str = "this is a test string";     System.out.println( findMaxLenEven(str));}} // This code is contributed by Arnab Kundu
# Python3 program to find maximum# length even word # Function to find maximum length# even worddef findMaxLenEven(str):    n = len(str)    i = 0     # To store length of current word.    currlen = 0     # To store length of maximum length word.    maxlen = 0     # To store starting index of maximum    # length word.    st = -1     while (i < n):         # 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.        if (str[i] == ' '):            if (currlen % 2 == 0):                if (maxlen < currlen):                    maxlen = currlen                    st = i - currlen             # Set currlen to zero for next word.            currlen = 0                 else :                         # Update length of current word.            currlen += 1         i += 1     # Check length of last word.    if (currlen % 2 == 0):        if (maxlen < currlen):            maxlen = currlen            st = i - currlen     # If no even length word is present    # then return -1.    if (st == -1):        print("trie")        return "-1"         return str[st: st + maxlen] # Driver codeif __name__ == "__main__":         str = "this is a test string"     print(findMaxLenEven(str)) # This code is contributed by Ita_c
// C# program to find maximum length even word using System; class GFG{         // Function to find maximum length even word    static String findMaxLenEven(string str)    {        int n = str.Length;             int i = 0;             // To store length of current word.        int currlen = 0;             // To store length of maximum length word.        int maxlen = 0;             // To store starting index of maximum        // length word.        int st = -1;             while (i < n)        {                 // 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.            if (str[i] == ' ')            {                if (currlen % 2 == 0)                {                    if (maxlen < currlen)                    {                        maxlen = currlen;                        st = i - currlen;                    }                }                     // Set currlen to zero for next word.                currlen = 0;            }            else            {                // Update length of current word.                currlen++;            }                 i++;        }             // Check length of last word.        if (currlen % 2 == 0)        {            if (maxlen < currlen)            {                maxlen = currlen;                st = i - currlen;            }        }             // If no even length word is present        // then return -1.        if (st == -1)            return "-1";             return str.Substring(st, maxlen);    }     // Driver code    public static void Main()    {        string str = "this is a test string";             Console.WriteLine(findMaxLenEven(str));    }    // This code is contributed by Ryuga}
<script>  // Javascript program to find maximum length even word // Function to find maximum length even wordfunction findMaxLenEven(str){    var n = str.length;     var i = 0;     // To store length of current word.    var currlen = 0;     // To store length of maximum length word.    var maxlen = 0;     // To store starting index of maximum    // length word.    var st = -1;     while (i < n) {         // 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.        if (str[i] == ' ') {            if (currlen % 2 == 0) {                if (maxlen < currlen) {                    maxlen = currlen;                    st = i - currlen;                }            }             // Set currlen to zero for next word.            currlen = 0;        }        else {            // Update length of current word.            currlen++;        }         i++;    }     // Check length of last word.    if (currlen % 2 == 0) {        if (maxlen < currlen) {            maxlen = currlen;            st = i - currlen;        }    }     // If no even length word is present    // then return -1.    if (st == -1)        return "-1";     return str.substr(st, maxlen);} // Driver codevar str = "this is a test string";document.write( findMaxLenEven(str)); // This code is contributed by noob2000.</script>
string
 
Time Complexity: O(N), where N is the length of the string. Auxiliary Space: O(1) 
andrew1234
ankthon
ukasp
ManasChhabra2
noob2000
array-traversal-question
Goldman Sachs
Data Structures
Strings
Goldman Sachs
Data Structures
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
What is Hashing | A Complete Tutorial
Introduction to Data Structures
Find if there is a path between two vertices in an undirected graph
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Different Methods to Reverse a String in C++