+ 1

🤔Can anyone tell me why work 2,3,4 are getting impacted by work1.

Separately they are working well but together value of work 1 is overriding the work 2,3,4. Please help😞 https://code.sololearn.com/czoEZvByJcFh/?ref=app

18th Jul 2022, 2:00 PM
Raisun Lakra
5 Answers
+ 1
don't use static for k and while( p[j] ){ valid but after that if ( p[ ++j ] .. May out of bounds.. so Reverse it while ( p[++j ] ){ if( p[j] == str[ ++i] )
18th Jul 2022, 5:14 PM
Jayakrishna 🇮🇳
+ 2
I didn't test this, but I think j should get reset to 0 in the beginning of the for loop in strCompare.
18th Jul 2022, 2:49 PM
Brian
Brian - avatar
+ 1
what do mean by overriding here..? What is your expected output? can you add clear details??
18th Jul 2022, 2:45 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 It's working thanks can u explain what was the problem there. Please
19th Jul 2022, 2:24 AM
Raisun Lakra
0
static variable only once initialized and retain its value between function call throughout program. And as already said, Ex: p=>"ABC", str => "zzABC", if p[ j ] = 'C' then if( p[ ++j ] == str[ ++i ] ) here j is out of bounds and beyond p length so value returned is \0 . If condition is false, else part reset k=0; But corrected code while( p[ ++j ] ) stops loop further... So k=1 at end. Hope it clears...
19th Jul 2022, 9:13 AM
Jayakrishna 🇮🇳