
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 <bits/stdc++.h> 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 < len) { if (str[i] == ' ') { if (currWordlen % 2 == 0) { if (maxWordLen < currWordlen) { maxWordLen = currWordlen; stringPointer = i - currWordlen; } } currWordlen = 0; } else { currWordlen++; } i++; } if (currWordlen % 2 == 0) { if (maxWordLen < currWordlen) { maxWordLen = currWordlen; stringPointer = i - currWordlen; } } if (stringPointer == -1) return "Not Found!"; return str.substr(stringPointer, maxWordLen); } int main() { string str = "Learn programming at Tutorialspoint"; cout<<"The maximum length even word is '"<<findMaxEvenLenWord(str)<<"'"; return 0; }
Output
The maximum length even word is 'Tutorialspoint'
- Related Articles
- Find the first repeated word in a string in Python?
- Find the first repeated word in a string in Java
- Find the first repeated word in a string in C++
- Python - Find the length of the last word in a string
- Get distinct first word from a string with MongoDB?
- Find the first repeated word in a string in Python using Dictionary
- Maximum even length sub-string that is permutation of a palindrome in C++
- Find First and Last Word of a File Containing String in Java
- Function to find the length of the second smallest word in a string in JavaScript
- PHP program to find the length of the last word in the string
- Reversing the even length words of a string in JavaScript
- Python program to print even length words in a string
- Print first letter of each word in a string in C#
- Write a program in Python to find the maximum length of a string in a given Series
- Check if a string contains a palindromic sub-string of even length in Python

Advertisements