+ 3

is there anything wrong in this code?help c++ i am getting wrong answer

#include <iostream> // std::cout #include <algorithm> // std::count #include<string> #include<vector> int main () { std::string s; std::cin>>s;//let s="SUVOSUVODKJS" std::vector<std::string>s1; s1.push_back(s); int m=std::count(s1.begin(),s1.end(),"SUVO"); std::cout<<m; return 0; }

20th Jun 2018, 10:09 AM
Bahubali
Bahubali - avatar
2 Answers
+ 5
The count() function returns a number of elements in a container that are equal to the item being compared. In your case, SUVO is not equal to SUVOSUVODKJS and thus the program returns a 0. Note that pushing a string into a vector would not split it into tokens which you need to match. You need to do that on your own. Though, there is a way by which you can count the number of occurrences of a substring inside a main string. https://code.sololearn.com/cr5J83i4fAlZ/?ref=app Now, if you execute: cout<<str_count("SUVOSUVODKJS","SUVO"); You will receive the output as 2.
20th Jun 2018, 1:19 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
thank u so much for ur time
20th Jun 2018, 1:27 PM
Bahubali
Bahubali - avatar