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

36 visualizzazioni (ultimi 30 giorni)
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');

Risposte (1)

Abhay Mohan
Abhay Mohan il 7 Dic 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

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by