You are using ||
..ie logical or
which evaluates to true
if any one of values is true
So for condition pre[i] != '\0' || str[i] != '\0'
, although pre[i]='\0'
, str[i]!='\0'
. So condition evaluates to true
and it enters the loop.
What you need to use is &&
..ie logical and
In simple terms, loop until both the strings are not '\0', This is achieved by
pre[i] != '\0' && str[i] != '\0'