Switch Names and Surnames Write a function SwitchNames that takes a n x 1 string
ID: 2249762 • Letter: S
Question
Switch Names and Surnames Write a function SwitchNames that takes a n x 1 string array, each cell consists of a name and a surname, and switch the names and surname, and insert a comma and space between the surname and the name. Restriction: The function must use the internal function split. For example listofNameSurnames listofNameSurnames ["Barney Google", "Snuffy Smith", "Dagwood Bumstead"] 3x1 string array "Barney Google" Snuffy Smith "Dagwood Bumstead" Matlab only > OutputStringsArray- SwitchNames (listofNameSurnames) OutputstringsArray 3x1 string array Google, Barney" "Smith, Snuffy "Bumstead, Dagwood" Your Function Save C Reset MATLAB Documentation 1 function OutputStringArray- SwitchNames (InputstringArray) 3OutputstringArray []; 4 data cellstr(InputstringArray); s for i -1:numel(data) 65 = data(i)(1:end); 7 splitNames strsplit(s); 8 str strcat (splitNames(2), {', '), splitNames(1)); 9 OutputstringArray = [OutputStringArray ; str]; 1e end 11 end 12 Code to call your function C Reset 1 OutputStringsArray- SwitchNames([ "Barney Google";"Snuffy Smith";"Dagwood Bumstead"])Explanation / Answer
% Script file for calling the functon: SwitchNames()
clc
clear all
close all
names=['Barney Google','Snuffy Smith','Dagwood Bumstead'];
switch_names=SwitchNames(names);
disp(switch_names);
% Function for Switch Name
function OutStringArray=SwitchNames(InputStringArray)
names1=split(InputStringArray);
switch_names=[name1(:,2) name1(:,1)];
switch_names=switch_names(:,1)+',';
switch_names=join(switch_names);
OutStringArray=switch_names;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.