If you want to remove spaces at both ends, it is probably simpler to trim the string once the user get out of the loop.
Alternatively, you might not fill the string with space at the beginning but add one character to the end of the string as appropriate and when the user press backspace remove last character.
Given that you already keep the count, the following might be what you want:
return A.substr(0, count);