I can't really answer without seeing the whole code .
I suspect the j , but again - we need to see the code.
anyway, I want to talk about SMART coding vs SYNTAXLY-CORRECT programming. although many lines of code are correct syntaxly - there will be better way to write them.
I work in programming company , and although I'm not a programmer, and the programming there is done with Java or Javascript/PHP , I get to talk with programmers with years of experience about programming.
nobody uses while loop. yes , I know I'll get crucified here with this - but it's true. while is uses very rarely, with very sparse situations , like wait for a socket to open , wait for something to finish download etc. almost 99% of the time you can exchange while loop with for loop .
for loop is a smarter decision because you know exactly how you start , what happens incrementally every loop and when to finish , it is also more readable.
let's try writing your code with for loop instead of while loop. as far as I understand, you want an array which will get the first letters which are the same in both strings. so we want to start with i=0, and we want to finish when the strings become different. in between we want to incrementally increase i and counter.
so we will get:
Code:
for (i=0,counter=0;word1[i]==word2[i];i++,counter++){
prefix[i]=word2[i];}
see? with 2 lines of code we wrote exactly the same as 14(!) lines of code, we better understood with what we start w, when we end and what happens in between.
isn't it much better now?