Hi! I tried to print words which end with "aty" and to count them using the find_last_of function. As you can see, there are four such words, but the execution result is
pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baly
Words ending with 'aty':
pryvitaty
zdaty
their number is 2
#include <iostream>
#include <string>
#include <vector>
#include<sstream>
usingnamespace std;
vector<string> Split(string strToSplit, char delimiter)
{
stringstream ss(strToSplit);
string item;
vector<string> splittedStrings;
while (getline(ss, item, delimiter))
{
splittedStrings.push_back(item);
}
return splittedStrings;
}
int main()
{
const string text = " pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysiki baly ";
const string end = "aty";
vector<string> words = Split(text, ' ');
cout<<"pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baly"<<endl;
int n = 0;
cout<<"Words ending with 'aty':"<<endl;
for(int i=0;i<words.size();i++) {
if(words[i].find_last_of(end,2)==2){
cout<<words[i]<<endl;
n++;
}
}
cout<<"their number is "<<n;
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
usingnamespace std;
vector<string> Split(const string& strToSplit, char delimiter)
{
stringstream ss(strToSplit);
vector<string> splittedStrings;
for (string item; getline(ss, item, delimiter); splittedStrings.push_back(item));
return splittedStrings;
}
int main()
{
const string text {" pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysiki baly "};
const string end {"aty"};
const vector<string> words {Split(text, ' ')};
cout << '\n' << text << "\nWords ending with '" << end << "'\n";
int n {};
for (constauto& w : words)
if (w.size() >= end.size())
if (constauto strt {w.size() - end.size()}; w.rfind(end, strt) == strt) {
cout << w << '\n';
++n;
}
cout << "their number is " << n;
}
or for C++20:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main()
{
const string text {" pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysiki baly "};
const string end {"aty"};
const vector<string> words {Split(text, ' ')};
cout << '\n' << text << "\nWords ending with '" << end << "'\n";
int n {};
for (constauto& w : words)
if (constauto strt {ssize(w) - ssize(end)}; (strt >= 0) && (w.rfind(end, strt) == strt)) {
cout << w << '\n';
++n;
}
cout << "their number is " << n;
}
Or alternatively, use .ends_with():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main()
{
const string text {" pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysiki baly "};
const string end {"aty"};
const vector<string> words {Split(text, ' ')};
cout << '\n' << text << "\nWords ending with '" << end << "'\n";
int n {};
for (constauto& w : words)
if (w.ends_with(end)) {
cout << w << '\n';
++n;
}
cout << "their number is " << n;
}
pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baly
Words ending with 'aty':
pryvitaty
pobazhaty
zdaty
otrymaty
their number is 4
#include <iostream>
#include <string>
#include <vector>
#include<sstream>
usingnamespace std;
vector<string> Split(string strToSplit, char delimiter)
{
stringstream ss(strToSplit);
string item;
vector<string> splittedStrings;
while (getline(ss, item, delimiter))
{
splittedStrings.push_back(item);
}
return splittedStrings;
}
int main()
{
const string text = " pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysiki baly ";
const string end = "aty";
vector<string> words = Split(text, ' ');
cout << "\n pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baly" << endl;
int wordCount = 0; // <--- A much better name than "n" which has no meaning.
cout << "\n Words ending with 'aty':\n";
for (size_t idx{}; idx < words.size(); idx++)
{
if (words[idx].find(end) != std::string::npos)
{
cout << " " << words[idx] << '\n';
wordCount++;
}
}
cout << "\n Their number is " << wordCount;
return 0; // <--- Not required, but makes a good break point.
}
Line 30 as an example use a good name over a single letter variable.
I revised the for loop a bit. The ".size()" function returns a "size_t" variable type, so it is best if the loop iterator matches, avoids the warning of a type mismatch.
The if state makes use of the find function, which is better than the way you are trying to use "find_last_of" which is checking from right to left.