How do you create a string from two strings by intersecting them char by char to form a new word using a loop?

1 view (last 30 days)
str1 = 'Hello'
str2 = 'Canada'
strNew = 'CHaenlaldoa' %This is how the new word should look like.
Thank you in advance!
  4 Comments

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 27 Oct 2018
Edited: Stephen23 on 27 Oct 2018
Loop not required, just use indexing:
Method one:
>> str1 = 'Hello';
>> str2 = 'Canada';
>> strN = [str1,str2];
>> strN([2:2:end,1:2:end]) = strN
strN = CHaenlaldoa
Method two:
>> strN = str2([1,1],:);
>> strN(1,2:end) = str1;
>> strN = strN(2:end)
strN = CHaenlaldoa

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!