+ 4
Not able to find the mistake done in the coding .Pls, help me in finding the error. https://www.sololearn.com/coach/77?ref=app
I was trying to solve the "security code coach problem" , so the code which i've written is running well for some cases while for others it is not. Like when i gave the input as 👉🏻xxGxx$T👈🏻 so, it should give "ALARM" as the output since 'G' is not present between '
#x27; and 'T' but idk why it is giving the output as "quiet". But if I'm giving 👉🏻GxxT$ , GT$ or xxxGGGxxxSxxGxxxTT 👈🏻 as inputs it is giving the desired output. So, what am i missing in my code b/c of which some inputs are running and some are not? This is the code which I've written 👇🏻 https://code.sololearn.com/cwRv7Dft17A8/?ref=app10 Respuestas
+ 1
Smriti Kaur
ch=format.charAt(I) ;
if(ch=='G')
{
int indG=format.indexOf(ch);
//here indG extract the first occurrence of Char 'G' which is 3 so not updating to next value.
But why need this to search again.. Here i value is the current character index. So
Put like G[b] =i; b++; is enough.
// G[b]=indG;
// b++;
Similarly in all calculations you are taking extra identifiers and calculations... So think about it, after you got results.. First now update above one correction...
You need same corrections for others if input have more than $T values... But there is no inputs with double other than G...
+ 3
Jayakrishna yeahhh rgt! After doing the above mentioned correction all the 6 cases matched.🎉
Thanks for helping 🙂
+ 2
Jayakrishna yes, u r rgt ...after doing that correction the code is running fine but now it is not running correctly for this input👉🏻xxxGGxx$xxGxxT👈🏻 as it is giving "ALARM" as the output although G is present between $ and T.
+ 2
Jayakrishna Oh yes sorry...
+ 2
Jayakrishna oh nice but what do u mean by saying giving values to G,T and $ ? Can u pls explain it a bit?
+ 2
Jayakrishna Oh okay now i got it ...cool ... n once again thank u 🎉
+ 1
Smriti Kaur
In third else part you should include as
else if(ch=='#x27;)
{
}
Also.. Otherwise other charecters are also included...
By your input, it having the values for G=3, $=1, T=6. So satisfying your condition..
lt;G<T because from different Arrays the values from.
There is need of 3 for loops actually, you can do it by one loop..
+ 1
Oh. Are you updated this code..? According to this am not getting Alarm...
+ 1
Smriti Kaur
You are Wel come..
And for this problem, I want mention alternative efficient way, which may help you later..
Just in one for loop
travesing though string element, give values to G, T, $.
After just put in if else..
Like if T<G<$ or lt;G<T then print quiet else Alarm...
+ 1
The alternative variables that you are taken as T[], G[], S[] Arrays..
G as guard, T as thief, $ as dollar...
Because only single value we need to compare. So arrays are not needed in this case..
If you try this way you need not all those 70+ lines of code. .. Simple code makes you rectify logic errors quickly...