简体   繁体   中英

Making numbers and letters valid and invalid in turing

So far I have numbers valid when you type in letters, but I don't know how to have numbers valid. Can I have help making numbers valid as well as letters?

var var_name : string

function check_letter (ch : string (1)) : int
    if ord (ch) >= 65 and ord (ch) <= 90 then
        result 1
    end if
    if ord (ch) >= 97 and ord (ch) <= 122 then
        result 1
    end if
    if  ord (ch) >= 0 and ord (ch) <= 9 then
        result 1
    end if
    result 0
end check_letter


put "please enter the postal code"
get var_name
for i : 1 .. length (var_name)
    if check_letter (var_name (i)) = 1 then
        put "postal code valid"
    else
        put "postal code invalid"
    end if
end for

(I'm honestly surprised anyone still uses Turing)

The random numbers ( 65 , 90 , 97 , 122 ) you have in your code are the letters' codepoints in ASCII. The codepoints of the digits 0-9 are not 0 - 9 like you have them there, they can be found an ASCII table instead: ASCII表

Alternatively, to make you code a bit cleaner, you could do this instead of hardcoding the codepoints:

if ord(ch) >= ord('A') and ord(ch) <= ord('Z') then
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM