Here is the code I have:
string words[n];
.
.
.
for(int j = 0; j < sizeof(words)/sizeof(words[0]); j++){
if(words[j].find(new_word) != std::string::npos){...} //abort(3) called
}
However, SIGABRT isn't called in the same code when I call find on a normal string, like
for(int j = 0; j < sizeof(words)/sizeof(words[0]); j++){
if(s.find(new_word) != std::string::npos){...} //abort(3) NOT called
Why is this behavior happening? Isn't words[j] referring to a string element? Edit: adding the entire code, in case the snippet isn't sufficient :
#include <stdio.h>
#include<string>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main() {
int T;
cin>>T;
while (T--){
int n;
cin>>n;
string words[n];
for(int i =0; i<n;i++){
cin>>words[i];
}
string s;
cin>>s;
string new_word;
//start inserting spaces now
for(int i =1;i<=s.length();i++){
new_word = s.substr(0,i);
//cout<<s<<endl;
//start scanning
for(int j = 0; j < sizeof(words)/sizeof(words[0]); j++){
if(words[j].find(new_word) != std::string::npos){
s = s.erase(s.find(new_word), new_word.length());
}
}
}
if(s.length() == 0)
cout<<"1"<<endl;
else
cout<<"0"<<endl ;
}
return 1;
}
Edit 2: The question is here :http://practice.geeksforgeeks.org/problems/word-break/0