accumulate with a Lambda
Completed
Read n words. Use accumulate with a lambda to build a single string of their initials in upper case, and again to compute the total number of letters.
Input
n, then n words.
Output
Two lines.
Example
Input
3 portable network graphics
Output
PNG 23
Editor
C++
Hints
accumulate(begin, end, start, op) folds the range into one value.
Solution explanation
The starting value decides the result type - here a string and a size_t.
Show solution
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> w(n);
for (auto &s : w) cin >> s;
string initials = accumulate(w.begin(), w.end(), string(),
[](string acc, const string &s) { return acc + static_cast<char>(toupper(s[0])); });
size_t letters = accumulate(w.begin(), w.end(), size_t{0},
[](size_t acc, const string &s) { return acc + s.size(); });
cout << initials << "\n" << letters << "\n";
return 0;
}