+ 1
Why my security problem code do not pass case4
#include <stdio.h> #include <string.h> int main() { char str[100], ch1,ch2; int i, len, flag=1; fgets(str,100,stdin); len=strlen(str); for(i=0;i<len;i++) { if((str[i]=='G')||(str[i]=='
#x27;)||(str[i]=='T')) { ch1=str[i]; ch2=str[i+1]; if(((ch1=='#x27;)&&(ch2=='T'))||((ch1=='T')&&(ch2=='#x27;))) flag =0; } } if(flag==0) printf("ALARM"); if(flag==1) printf("quiet"); return 0; }3 Respostas
+ 5
Please carefully read the challenge again.
$ and T don't have to be direct neighbours.
+ 2
with ch2 = str[i +1]; you are assuming that there is no 'x' between the 'G' 'T' or '#x27;
and your for loop doesn't stop when if conditions are met, it just keep looping to the end.
your approach would work if you eliminate all 'x' that way you are sure '#x27; is directly next to 'T' or 'G'.
so you can remove all 'x' and locate '#x27; index then check if there is a 'T' in index + 1 or index - 1.
0
Ssss
something like, just bellow len =strlen(strl);:
int j = 0;
char nstr[100];
//remove 'x' an copy result to nstr
for(i = 0 ; i < len;i++){
if (str[i] != 'x'){
nstr[j] = str[i];
j++;
}
}
//find index of '#x27; and save it in s_index
int s_index = 0;
for(int i =0; i < strlen(nstr);i++){
if(nstr[i] == '#x27;){
s_index = i;
break;
}
}
//check left and right of '#x27;
if (nstr[s_index -1] == 'T' || nstr[s_index + 1] == 'T'){
printf("ALARM");
}
else{
printf("quiet");
}
return 0 ;