Solved how to search string in c++

June 5, 2011 at 06:59:00
Specs: Windows XP, .99 MB
How to search a string in text file ..
plz help and write a function to search.....//

See More: how to search string in c++

Report •

June 5, 2011 at 14:41:50
✔ Best Answer
std::string s("some text to search for");

if(s.find("text to search") != std::string::npos)
   // "text to search" was found in string
   // "text to search" wasn't found in string

I guess... your question is sort of vague.

You've also got substr that you can employ; as for the file, if you're reading lines, use getline(std::ifstream_obj, std::string_var) and perhaps some flavor of the above. If you're breaking on whitespace, use the extraction operator >> (std::ifstream_obj >> std::string_var) and perhaps a comparison test std::string_var == some_string

As for a function, it depends on what you're wanting to do, I think... if the string is present, then perhaps return a boolean; if you want its position within a string, then perhaps return an integer; if you want the string, then return a string.


Report •

June 9, 2011 at 02:19:24
thanks for your solution ... it helps me.....

Report •

Related Solutions

Ask Question