Borrar filtros
Borrar filtros

Judging input word based on first letter

2 visualizaciones (últimos 30 días)
Andrew Wiebe
Andrew Wiebe el 4 de Oct. de 2015
Respondida: Walter Roberson el 4 de Oct. de 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!

Respuestas (1)

Walter Roberson
Walter Roberson el 4 de Oct. de 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

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by