How to count the number of characters in array of strings with C++

1 Answer

0 votes
#include <iostream>
#include <numeric>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> arr = {"c#", "c", "java", "python", "c++"};

    int totalCharacters = std::accumulate(arr.begin(), arr.end(), 0, [](int sum, const std::string& s) {
        return sum + s.length();
    });

    std::cout << totalCharacters << std::endl;
}

 
/*
run:
 
16
 
*/

 



answered Aug 20, 2024 by avibootz
...