How to count the number of words in a string that end with s or y in C++

1 Answer

0 votes
#include <algorithm>
#include <iterator>
#include <iostream>
#include <sstream>
#include <string>

int main() {
    
    std::string str = "c lens c++ news java enjoy chess c# money";

    std::istringstream iss{ str };

    int count = std::count_if(std::istream_iterator<std::string>(iss), {},
                    [](const std::string& s) { return s.back() == 's' || s.back() == 'y'; });
    
    std::cout << count;
}


 
/*
run:
 
5
 
*/

 



answered Jan 4, 2024 by avibootz
...