how to create a function that gets input as string and display every character
36 views (last 30 days)
Show older comments
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');
0 Comments
Answers (1)
Abhay Mohan
on 7 Dec 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
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!