stack overflow - process is terminated due to StackOverflowException from bool function C++ -
i trying make bool function allow input of string , input of substring searched in string's input. bool function should search recursively match, return true if there match inside of string. example: 'word' entered string, 'or' substring looking inside string. function should return true since 'or' inside 'word'. when run code, on command line say, "process terminated due stackoverflowexception" confused error means , how relates code.
#include <iostream> #include <string> using namespace std; bool find(string s, string t) { if(s.length() <= 1){ return false; } int t_len = t.length() - 1; string se = s.substr(0, t_len); if(se == t){ return true; } else s.erase(0,0); return find(s, t); } int main() { string s; string t; cout << "enter string s: " << endl; cin >> s; cout << "enter string t: " << endl; cin >> t; bool is_found = find(s, t); if(is_found = true) { cout << "found: " << t << " in " << s << endl; } system("pause"); return 0; }
your call erase not deleting characters. definition of erase is:
erase( size_type index = 0, size_type count = npos );
so want erase 1
character, not 0
characters.
additionally, test @ end should read:
if(is_found == true)
as otherwise, you're trying assign is_found value true , test value (which true in case.
additionally, have:
string se = s.substr(0, t_len);
where t_len
incorrect (off one), string comparison not work.
fundamentally, looks you've not understood definitions of erase
or substr
, essential getting work properly.
Comments
Post a Comment