Trying to count the number of character in a string
I think the problem has to do with the definition of CHAR(..).  It includes trailing blanks to fill out the space indicated.  But the actual string is does not have any trailing spaces.  So ' ' becomes ''.

So, change from str CHAR(1) to str VARCHAR(1).