Judging input word based on first letter

2 views (last 30 days)
Andrew Wiebe
Andrew Wiebe on 4 Oct 2015
Answered: Walter Roberson on 4 Oct 2015
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!

Answers (1)

Walter Roberson
Walter Roberson on 4 Oct 2015
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

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!