Hello all,
I am trying to write a function which takes a string as input and converts it to Pig Latin string. For example, 'pig latin in matlab' would become 'igpay atinlay inay atlabmay'. So everything before the first vowel is shifted to the end of the word and following by 'ay'.
I know that I can use find to find the first vowel in each word. But then how do I move the characters to the end of each word and append 'ay' to it?
Here is what I have so far:
function [ new_string ] = pig_latin( string )
each_word = [];
temp = '';
ay = 'ay';
words = strsplit(string,' ');
num_words = numel(words);
for j=1:num_words
temp = words{j};
vowel = temp(find(ismember(temp,'aeiou'),1,'first'))
vowel_loc = find(vowel,1,'first');
end

 채택된 답변

KSSV
KSSV 2016년 11월 24일

0 개 추천

clc; clear all ;
str = 'pig latin in matlab' ;
C = strsplit(str) ; % split the string
%%chnage accordinhly
s0 = 'ay' ; % which has to be joined
l = cellfun(@(c)[c(2:end) c(1)],C,'uni',false) ;
m = cellfun(@(c)[c s0],l,'uni',false) ;
iwant = strjoin(m)

댓글 수: 6

Scott
Scott 2016년 11월 24일
That works great, but can you please explain how each of those commands work?
KSSV
KSSV 2016년 11월 24일
You can read the respective documentation....
[x y] works like joining two strings, where x and y are two strings. cellfun applies a specified function to all the cells.
David Davis
David Davis 2016년 11월 25일
편집: David Davis 2016년 11월 25일
this isn't perfect though... if you start a word with a vowel like "other" it returns theroay
KSSV
KSSV 2016년 11월 25일
What it should be?
Image Analyst
Image Analyst 2016년 11월 25일
Is this solved yet? You accepted this answer but seem to indicate it's not right. What about my answer below? Is that right?
KSSV
KSSV 2016년 11월 25일
vowels = {'a' 'e' 'i' 'o' 'u'} ;
str = 'pig latin in matlab' ;
C = strsplit(str) ; % split the string
l = cellfun(@(c) c(1),C,'uni',false) ;
% get vowels pos
idx = ismember(l,vowels) ;
C1 = C(~idx) ;
%%chnage accordinhly
s0 = 'ay' ; % which has to be joined
l1 = cellfun(@(c)[c(2:end) c(1)],C1,'uni',false) ;
m1 = cellfun(@(c)[c s0],l1,'uni',false) ;
%%work on vowel
C2 = C(idx) ;
m2 = cellfun(@(c)[c s0],C2,'uni',false) ;
%%Replace strings back
m = cell(size(C)) ;%
m(~idx) = m1 ;
m(idx) = m2 ;
iwant = strjoin(m)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 11월 25일

26 개 추천

How about this:
To call:
new_string = pig_latin('pig latin in matlab other')
Function declaration
function [new_string] = pig_latin( string )
new_string = '';
ay = 'ay';
words = strsplit(string,' ');
numWords = length(words);
for k = 1 : numWords
thisWord = words{k};
firstVowel = thisWord(find(ismember(thisWord,'aeiou'),1,'first'));
vowelLocation = strfind(thisWord, firstVowel);
if vowelLocation >= 2
% Start the new word at the vowel location.
newWord{k} = [thisWord(vowelLocation:end), thisWord(1:vowelLocation-1), ay, ' '];
else
% If word starts with a vowel, just add 'ay'.
newWord{k} = [thisWord, ay, ' '];
end
end
% celldisp(newWord);
% String cells together to form the output character array.
new_string = [newWord{:}];
Shows in command window:
new_string =
igpay atinlay inay atlabmay otheray

댓글 수: 2

Scott
Scott 2016년 11월 25일
That is perfect! It works great, and uses things that I know in Matlab! Thankyou image analyst!
Image Analyst
Image Analyst 2016년 11월 25일
You're welcome. I tried to keep it somewhat simple and straightforward, and on your level and similar to your existing code. Maybe you could at least "Vote" for my answer.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2016년 11월 24일

댓글:

2016년 11월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by