Alright i am using the Find method to look in the users input, for certain words , but the code is long and kind of an eye sore , does anyone know of a way to write the code and make it shorter.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
std::string str ("There are two needls in this haystack with a needle.");
std::string str2 ("There");
std::string str3 ("needle");
std::string str4 ("in");
std::string str5 ("haystack");
std::size_t found = str.find(str2);
found = str.find(str3);
found = str.find(str4);
found = str.find(str5);
if (found!=std::string::npos)
{
// do something
}
system("pause");
return 0;
}
#include <windows.h>
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string str ("There are two needls in this haystack with a needle.");
string str2 ("There");
string str3 ("needle");
string str4 ("in");
string str5 ("haystack");
size_t found = str.find(str2);
found = str.find(str2);
found = str.find(str2);
found = str.find(str2);
found = str.find(str2);
if (found!=string::npos)
{
// do something
}
system("pause");
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main (){const string str = "There are two needles in this haystack with a needle." ;
for( const string token : { "There", "needle", "in", "haystack" } ){if( str.find(token) != stdstring::npos ){/* do something*/}}}
If you're talking about the code by jasonwynn10, then yes there is at least one error. I suggest you use the code provided by JLBorges. And please please don't try to stuff so many things on so few lines as shown by jasonwynn10, it'll just make your programs harder to read and almost impossible to troubleshoot.
well with the code given by JLBorges i am getting a error right after the token : of expected an expression and i have tried everything i know of and it wont go away
1 2 3 4 5 6 7 8 9
const std::string str = "There are two needles in this haystack with a needle." ;
for( const std::string token : {"There", "needle", "in", "haystack" } )
{
if( str.find(token) != std::string::npos )
{
// do something
}
}
#include <regex>
#include <string>
int main()
{
const std::string sample = "There are two needles in this haystack with a needle."const std::string regex_str = "(there|needle|in|haystack)";
std::regex example_regex(regex_str, regex_constants::icase);
if(std::regex_search(sample, example_regex))
{
//Do something
}
return 0;
}
Not tested, but should work with minor or no changes. The regex_constants::icase just says to ignore case. So it'll match There, there, tHere, tHeRe, etc.
Resource for building and testing regular expressions: http://www.regexr.com/
Works for me. After actually going and reading the thread, your compiler may not actually support enough of C++11 to use <regex>, though. If you can grab an up-to-date version of Visual Studio, this will work. It ran just fine on my VS2013 Professional. You can get Professional for free with a *.edu email address, or if you're not a student you can get 2013 Express for free.
I highly recommend updating so you can use modern C++ features.
It doesn't have equivalent functionality: there is no std::match_results<> object to hold the sub-matches and there is no loop to iterate through the sub-matches.
If OP was just wanting to see if any of these strings were a substring of the first string, then what I posted works and is probably the simplest and cleanest solution.
#include <iostream>
#include <string>
usingnamespace std;
int main (){const string str = "There are two needles in this haystack with a needle." ;
for( const string token : { "There", "needle", "in", "haystack" } ){if( str.find(token) != stdstring::npos ){/* do something*/}}}
Please use this as an example what NOT to do. Never put your main function in one line.