how to write a function that receives a word as input and returns a pig Latin translation of the word. Both the input word and the pig Latin word are character arrays. The pig Latin translation involves first determining if the input word begins with a vowel (a, e, i, o, or u). If so, append 'way' to form the pig Latin translation. If the input word does not begin with a vowel, then form the pig Latin translation by moving the first letter to the end of the word and then appending 'ay'. I also want to include a help option, so when the user types in "help" they get my pre written message

5 件のコメント

Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
Hi Charlotte, is this a homework question? Did you try some code and struck somewhere?
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
I'm trying right now, it's a practice question my prof provided
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
Sure. That's good. Show till what you tried and that can be helped when struck. For some insights to get started look at this page https://www.mathworks.com/help/matlab/functions.html
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
I found that site and got the help sentence coded, but I'm confused on how to actually make the commands for the vowels vs nonvowels and how they'll code
Stephen23
Stephen23 2020 年 3 月 19 日
編集済み: Stephen23 2020 年 3 月 19 日
@Charlotte Reed: you could do this using indexing, ismember, and if. Give it a try!

サインインしてコメントする。

 採用された回答

Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日

0 投票

Hi Charlotte,
Here is how you can perform this. Since you need to check if the first letter of the input word is vowel or not, you can use strcmp and strcmpi functions.
As you written the text already, i am just showing the way you can code it as below:
function out = pigLatinTranslation(word)
% Check if the first letter of the word is vowel or not
if any(strcmpi(word(1),{'a','e','i','o','u'}))
out = [word 'way']; % Append 'way' to the word
else
out = [word(2:end) word(1) 'ay']; % Place the starting letter at the end and append the 'ay'
end
end
This when tried as:
>> pigLatinTranslation('pple')
ans =
'plepay'
>> pigLatinTranslation('apple')
ans =
'appleway'
Hope this helps.
Regards,
Sriram

11 件のコメント

Stephen23
Stephen23 2020 年 3 月 19 日
Simpler with ismember:
if ismember(word(1),'aeiouAEIOU')
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
Thank you so much Sriram! It says there are not enough input arguements when I try to run it though?
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
Run it by passing a word to the function as provided in the answer.
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
I would like to have a line for the user to input a word, and then the code reacts for the piglatin answer. I'm not too sure what you mean by this?
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
Sure.. Then update the above as such:
function out = pigLatinTranslation
word = input('Enter a word:','s');
... % Then continue with the other commands in the answers
% In the if condition, you could even use what Stephen as suggested
end
Hope this helps.
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
Now use it directly with function name word2piglatin without any inputs
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
Sorry, what do you mean by that?
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
In the command window, directly run
>> word2piglatin
% This will prompt you to enter a word, then provide any character vector and then press enter
% you will get the output
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
Got it!!! Thank you so so so much! :)
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
How would the code have to change for a phrase and wanting to translate it into pig latin?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by