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

36 vues (au cours des 30 derniers jours)
Ira Wal
Ira Wal le 7 Déc 2020
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');

Réponses (1)

Abhay Mohan
Abhay Mohan le 7 Déc 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

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by