fix seg fault when string has trailing spaces