Hello, I don't know how to find the first string in array with longest length? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hello, I don't know how to find the first string in array with longest length?

https://code.sololearn.com/c8CNDCyxVUro/?ref=app

30th Nov 2022, 6:31 AM
TeaserCode
2 Answers
+ 1
Why that much complicating the task..? Simply iterate vector through loop And take each string into a string variable string s = vector[i]; int len = s.length() ; Then find is it maximum! If maximum swap with previous values.. Finally print max length string... edit : TeaserCode std::string LongestConsec::longestConsec(const std::vector<std::string> &strarr, int k) { std::string result; int d = strarr.size(), max=0; for( int i=0; i<d; i++ ) { std::string s = strarr[i]; int l = s.length(); if(max <= l){ max = l; result = s; } } return result; }
30th Nov 2022, 10:39 AM
Jayakrishna 🇮🇳