필터 지우기
필터 지우기

word input codes in matlab

조회 수: 5 (최근 30일)
Charlotte Reed
Charlotte Reed 2020년 3월 19일
댓글: Charlotte Reed 2020년 3월 19일
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
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by