The problem is that you copy the characters from
arr
to
str
, and stop (correctly) when the null character is reached. However, you forget to add a null character at the end of the newly created string. It should be:
while (arr[arrPosition] != '\0')
{
if (arr[arrPosition] != ' ')
str[strPosition++] = arr[arrPosition];
arrPosition++;
}
str[strPosition] = '\0';
By the way, what compiler are you using, as this will not compile under Microsoft's C++ compiler, owing to the strange syntax on some statments?