Print all strings of maximum length from an array of strings | C ++ program to implement the above approach ; Function to find the length of the longest string from the given array of strings ; Traverse the array ; Stores the length of current string ; Update maximum length ; Return the maximum length ; Function to print the longest strings from the array ; Find the strings having length equals to len ; Print the resultant vector of strings ; Function to print all the longest strings from the array ; Find the length of longest string ; Find and print all the strings having length equals to max ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( vector < string > arr ) { int len = INT_MIN ; int N = arr . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] . size ( ) ; if ( len < l ) { len = l ; } } return len ; } void maxStrings ( vector < string > arr , int len ) { int N = arr . size ( ) ; vector < string > ans ; for ( int i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . size ( ) ) { ans . push_back ( arr [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } void printStrings ( vector < string > & arr ) { int max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } int main ( ) { vector < string > arr = { " aba " , " aa " , " ad " , " vcd " , " aba " } ; printStrings ( arr ) ; return 0 ; }