Find the first maximum length even word from a string in C++

In this problem, we are a string str consisting of comma separated words. Our task is to find the first maximum length, even word, from a string.

We need to find the largest word 'string between two spaces' whose length is maximum and even.

Let's take an example to understand the problem,

Input : str = "learn programming at TutorialsPoint"
Output : TutorialsPoint

Explanation

The string with even length is TutorialsPoint.

Solution Approach

A simple solution to the problem is by simply finding the string which has even length greater than the current string. Initialise the maxString length to 0.

Algoritham

Step 1 − Iterate over the string.

Step 2 − check if the current word has even length and the length of word is greater than last greater word.

Step 3 − Return the word.

Example

Program to illustrate the working of our solution

#include 
using namespace std;
string findMaxEvenLenWord(string str) {
   int len = str.length();
   int i = 0; int currWordlen = 0;
   int maxWordLen = 0;
   int stringPointer = -1;
   while (i 

Output

The maximum length even word is 'Tutorialspoint'
Updated on: 2022-01-27T10:43:34+05:30

942 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements