6.5 String Elements LAB ACTITY 6.5.1: String Elements This tool is provided by a
ID: 3588950 • Letter: 6
Question
6.5 String Elements LAB ACTITY 6.5.1: String Elements This tool is provided by a third party Your activity is always but you may need to refresh the page to fill in the banner String Elements You can get the individual characters from a string just as you would from an array! The same syntax applies. Wnite a function that takes a string as an input and returns the first character of the string as an output. You can assume that the string has at least one character. Your Function Save eReset MATLAB Documentation 1 function y firstChar(x) yxi 3 end Code to call your function C Reset 1 chari firstChar Hellot char2 firstChar( 'Neato') Run Function Output Submit Assessment Function TestExplanation / Answer
%save as firstChar.m
function y=firstChar(x)
%get first character from vector
y=x(1);
end
--------------------------------------------------------------------------------
Sample output:
char1 =
H
--> char1=firstChar('Hello')
char1 =
H
--> char2=firstChar('Neato')
char2 =
N
---------------------------------------------------------------------------------------
%
%save as firstLast.m
function out=firstLast(in)
%empty vector
out=[];
%get length
l=length(in);
%check if length is 1
if l==1
out=in(1);
else
%otherwise set first and last element in
%vector out
out=[in(1) in(l)];
end
end
---------------------------------------------------
Sample Output:
--> test1=firstLast(4)
test1 =
4
--> test2=firstLast([10 9 8 7 6 5 4 3 2 1])
test2 =
10 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.