Kodewick
Start learning →
Basic←→0 / 60

Longest name

#014BasicLoopsStrings4 min

Before making name badges, find the longest name to see how wide they must be. Write longest_name(names) to return the longest name; if two are just as long, keep the earlier one. No names gives an empty string "".

Examples
longest_name({"Amy", "Benjamin", "Zoe"})→'Benjamin'
longest_name({})→''
The functionstring longest_name(const vector<string> &names)
main.cpp
#include <string>
#include <vector>
using namespace std;

string longest_name(const vector<string> &names) {
    string best = "";
    for (const string &name : names) {
        // your code here: if name is longer than best, make best name
    }
    return best;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.