Policz to z pomocą std::count_if(...) z <algorithm>.
Nieco nowocześniej napisane..:
#include <string>
#include <cctype>
#include <algorithm>
#include <iostream>
// C++14
using namespace std;
using namespace std::string_literals;
auto policzSamogloski(const string& tekst)
{
const auto samogloski = "aeiou"s;
return count_if(tekst.cbegin(), tekst.cend(),
[&samogloski](const int c) {
return samogloski.find(static_cast<char>(::tolower(c))) != string::npos;
});
}
int main(){
string tekst;
cout << "Podaj tekst: ";
getline(std::cin, tekst);
cout << "Liczba samoglosek wynosi: "
<< policzSamogloski(tekst) << '\n';
}
I dla nieco starszych kompilatorów:
#include <string>
#include <cctype>
#include <cstddef>
#include <algorithm>
#include <iostream>
// C++03
using namespace std;
bool czySamogloska(const char c) {
const string samogloski = "aeiou";
return samogloski.find(static_cast<char>(::tolower(c))) != string::npos;
}
size_t policzSamogloski(const string& tekst)
{
return static_cast<size_t>(count_if(tekst.begin(), tekst.end(), czySamogloska));
}
int main(){
string tekst;
cout << "Podaj tekst: ";
getline(std::cin, tekst);
cout << "Liczba samoglosek wynosi: "
<< policzSamogloski(tekst) << '\n';
}