我必须将字符串上的每个元音都改为大写或小写,这取决于它已经是什么..所以“UPPERCASE小写”变成“uPPeRCaSe lOwErcAsE”
到目前为止,我对这种方法没有成功
str= 'UPPERCASE lowercase';
vow = 'aeiou';
vowm = 'AEIOU';
for k = 1:5
if str(str == vow(k))
str(str == vow(k))= vowm(k);
else
if str(str == vowm(k))
str(str == vowm(k))= vow(k);
预期产出:“uPPeRCaSe lOwErcAsE”
实际输出:“uPPERCASE lOwErCAsE”
我对matlab非常新,我有点失落。 我恭喜你的帮助
答案 0 :(得分:5)
使用ismember
查找所有类型的元音(大写或小写),然后upper
和lower
进行转换:
str = 'UPPERCASE lowercase'; %// original string
indl = ismember(str, 'aeiou'); %// locations of lowercase vowels
indu = ismember(str, 'AEIOU'); %// locations of uppercase vowels
str(indl) = upper(str(indl)); %// convert from lower to upper
str(indu) = lower(str(indu)); %// convert from upper to lower
答案 1 :(得分:3)
如问题中所列,我假设以下为输入 -
%// Inputs
str= 'UPPERCASE lowercase'
vow = 'aeiou'
vowm = 'AEIOU'
方法#1
一种基于changem
的方法,用于替换值 -
%// Create maps from input string to reflect changes from lower to upper
%// and vice versa
map1 = changem(str,vowm,vow)
map2 = changem(str,vow,vowm)
%// Find indices to be changed for lower to upper change and vice versa change
idx1 = find(map1~=str)
idx2 = find(map2~=str)
%// Selectively change input string based on the indices to be changed and maps
str(idx1) = map1(idx1)
str(idx2) = map2(idx2)
方法#2
使用bsxfun
-
%// Find indices to be changed for lower to upper change and vice versa change
[~,idx1] = find(bsxfun(@eq,str,vow'))
[~,idx2] = find(bsxfun(@eq,str,vowm'))
%// Selectively change input string based on the indices to be changed and maps
str(idx1) = str(idx1)-32
str(idx2) = str(idx2)+32
答案 2 :(得分:2)
您可以使用 regular expressions 同样。
我不知道这与其他答案有多么不同......
str= 'UPPERCASE lowercase';
vow = '[aeiou]';
vowm = '[AEIOU]';
indl = regexp(str,vow);
indu = regexp(str,vowm);
str(indl) = upper(str(indl));
str(indu) = lower(str(indu));