Why does my code not work out well replacing strings characters