Judging input word based on first letter

2 次查看(过去 30 天)
How would I write an m-file that asks a user to input a word and only returns a correct response if the word begins with "A","B","C".
x=input('Please input a single word surrounded by parentheses.')
case(first letter begins with A,B,C)
disp(Input word is correct.)
case(first letter does not begin with A,B,C)
disp(Input is incorrect. Please input a correct word.)
end
thanks!

回答(1 个)

Walter Roberson
Walter Roberson 2015-10-4
Use the 's' option of input(). Subscript to get the first letter. switch() with case {'A', 'B', 'C'}
Question: what about 'a', 'b', 'c' ?
Watch out for the user pressing return without entering a word

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by