how to create a function that gets input as string and display every character

46 visualizaciones (últimos 30 días)
Could you please help me to create a function that gets input as a string and then dispaly every character separately, e.g
input('abcd')
display:
first character is: a
second character: b
third character is: c
fourth character is: d
code
prompt = 'Enter a word: ';
str = input(prompt,'s');

Respuestas (1)

Abhay Mohan
Abhay Mohan el 7 de Dic. de 2020
This code will give a slightly modified version of what you want to achieve. Instead of First, Second, Third and Fourth, it will out put as 'Character number 1 is: ' , 'Character number 2 is: ' and so on. So this will work no matter how big the string is. If you want an output exactly like how you have asked, you can use a similar logic, but with a pre saved list of qualifiers {'First','Second','Third',...}
prompt = 'Enter a word: ';
str = input(prompt,'s');
for ii = 1:length(str)
str1 = join({'Character number ',num2str(ii),' is: ',str(ii)},'');
disp(str1{1});
end

Categorías

Más información sobre Characters and Strings 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