How do I use input to make a string and fprintf to output different parts of the string?
Mostrar comentarios más antiguos
I'm trying to write a program that will allow the user to enter their name as a string. And then using fprintf, output the user’s name as well as the first and last letter of the users name separately.
3 comentarios
James Tursa
el 2 de Abr. de 2018
Please show a short example of an input and the exact desired output.
Sydney Mukavetz
el 2 de Abr. de 2018
Bob Thompson
el 2 de Abr. de 2018
Editada: Bob Thompson
el 2 de Abr. de 2018
The portion of the input should work fine (EDIT: Nvm, listen to James answer). If you want just first and last letters then you should be able to save them as different values using indexing.
name = input('Enter your name: ');
first = name(1);
last = name(end);
fprintf('Your name is: %s %nWith first and last letters: %c and %c%n',name,first,last);
The exact syntax may be a bit off since I don't always remember the exact nuances of fprintf, but that should be pretty much what you want.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!