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

75 次查看(过去 30 天)
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');

回答(1 个)

Abhay Mohan
Abhay Mohan 2020-12-7
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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by