When you don't initialise the variable it has a random value, whatever was in the memory address, so while (i <=strlen(str)) is going to behave unpredictably.
You should always initialise.
(Visual Studio Debug configurations automatically initialise variables.)