How to compute word scores in Scrabble using MATLAB

前端 未结 2 1989
感动是毒
感动是毒 2020-12-20 02:16

I have a homework program I have run into a problem with. We basically have to take a word (such as MATLAB) and have the function give us the correct score value for it usi

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 02:47

    Convert from string to ASCII:

    >> myString = 'hello, world';
    >> ASCII = double(myString)
    
    ASCII =
    
       104   101   108   108   111    44    32   119   111   114   108   100
    

    Sum up the values:

    >> total = sum(ASCII)
    
    total =
    
            1160
    

    The MATLAB help for char() says (emphasis added):

    S = char(X) converts array X of nonnegative integer codes into a character array. Valid codes range from 0 to 65535, where codes 0 through 127 correspond to 7-bit ASCII characters. The characters that MATLAB® can process (other than 7-bit ASCII characters) depend upon your current locale setting. To convert characters into a numeric array, use the double function.

    ASCII chart here.

提交回复
热议问题