Write a function catit1 that will receive one input argument, which is a cell array. If the cell array contains only strings, it will return two strings : one string is all of the strings from the cell array concatenated together, the other string is
Show older comments
Question: Write a function catit1 that will receive one input argument, which is a cell array. If the cell array contains only strings, it will return two strings : one string is all of the strings from the cell array concatenated together, the other string is combing the last alphabets of each word in cell array. Otherwise, it will return two empty strings.
>>fishies={‘tuna’, ‘shark’, ‘salmon’}
>>[str1,str2]=catit1(fishies)
str1= ‘tunasharksalmon’
str2= ‘akn’
I am having trouble figuring out how to get the strings to both concatenate and encrypt. Without the str2 input at the bottom, the str1 concatenation works fine.
fishies = {'tuna','shark','salmon'};
[str1,str2] = catit1(fishies);
function [str1,str2] = catit1(ca)
str1 = '';
if iscellstr(ca)
for i = 1:length(ca)
str1 = strcat(str1,ca{i});
end
end
car = char(ca);
rest = strtrim(car);
str2 = '';
while ~isempty(rest)
[word,rest] = strtok(rest);
str2 = strcat(str2,word(end));
end
end
Answers (1)
Stephen23
on 4 Apr 2019
Simpler without loops:
>> C = {'tuna','shark','salmon'};
>> horzcat(C{:}) % comma-separated list
ans = tunasharksalmon
>> cellfun(@(v)v(end),C)
ans = akn
Read more about how this works:
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!