0
Help with Security
I need help with security problem #include <iostream> #include <deque> using namespace std; int main() { char a[100]; deque<char>d; for(int i=0;i<100;i++){ cin>>a[i]; if(a[i]=='G'||a[i]=='T'||a[i]=='
#x27;){ d.push_back(a[i]); } } for(int i=0;i<d.size();i++){ if(d[i]=='#x27;){ if(d[i-1]=='T'||d[i+1]=='T'){ cout<<"ALARM"; }else{ cout<<"quiet"; } } } } Test case number 5 and 6 don't work2 odpowiedzi
+ 2
You should get rid of the undefined behaviour in your program. Not only are you potentially reading too many characters and accessing uninitialized characters in the array, you are also accessing the deque out of bounds if the '#x27; is at the front or the back.
0
#include <iostream>
#include <deque>
using namespace std;
int main() {
char a[100];
deque<char>d;
for(int i=0;i<100;i++){
cin>>a[i];
if(a[i]=='G'||a[i]=='T'||a[i]=='#x27;){
d.push_back(a[i]);
}
}
for(int i=0;i<d.size();i++){
if(d[i]=='#x27;){
if(i==0){
if(d[i+1]=='T'){
cout<<"ALARM";
}else{
cout<<"quiet";
}
}else if(i+1==d.size()){
if(d[i-1]=='T'){
cout<<"ALARM";
}else{
cout<<"quiet";
}
}else{
if(d[i-1]=='T' || d[i+1]=='T'){
cout<<"ALARM";
}else{
cout<<"quiet";
}
}
}
}
}
I modified the code following your advice and now it works, 6 test cases out of 6, thank you very much for your help