Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Write a function that translates a string of English words to a string of

ID: 3778052 • Letter: M

Question

MATLAB Write a function that translates a string of English words to a string of Pig Latin words.

>> pig_latin('pig latin in matlab') ans = igpay atinlay inay atlabmay >> pig_latin('engineers rule the world') ans = engineersay uleray ethay orldway

To translate a word into Pig Latin, move all letters up to, but not including the first vowel (a,e,i,o,u), to the end of the word and append the -ay suffix. x Pig Æ igpay x Latin Æ atinlay x School Æoolschay x In Æinay x Out Æ outay x Yes Æ esyay

Explanation / Answer

%pig_latin.m file

function [new_string] = pig_latin(str)
new_string = '';
ay = 'ay';
words = strsplit(str,' ');
wordLength = length(words);
for k = 1 : wordLength
eachWord = words{k};
first_vowel = eachWord(find(ismember(eachWord,'aeiou'),1,'first'));
vowel_ind = strfind(eachWord, first_vowel);
   if vowel_ind >= 2
       % start new word at vowel index
       new_word_arr{k} = [eachWord(vowel_ind:end), eachWord(1:vowel_ind-1), ay, ' '];
   else
       % add 'ay' if word starts with vowel
       new_word_arr{k} = [eachWord, ay, ' '];
   end
end
% celldisp(new_word_arr);
% String cells together to form the output character array.
new_string = [new_word_arr{:}];

% demo.m file

disp(pig_latin('engineers rule the world'));

%output

%engineersay uleray ethay orldway