【MATLAB编程实例练习】-(36)删除所有以“ain”结尾的单词

news/2024/11/16 3:42:39/

题目

来源于Mathwork上的Cody,Problem 31 - Remove all the words that end with “ain”.

Given the string s1, return the string s2 with the target characters removed.
For example, given
s1 = ‘the main event’
your code would return
s2 = ‘the event’
Note the 2 spaces between “main” and “event” Only the four letters in the word “main” were deleted.

代码

function s2 = remAin(s1)ain_index=strfind(s1,'ain');del_index=[];for i=1:length(ain_index)if ain_index(i)+2==length(s1) %整个字符串以ain结尾的情况del_index=[del_index,max(strfind(s1(1:ain_index(i)),' '))+1:ain_index(i)+2];elseif s1(ain_index(i)+3)==' ' %单词以ain结尾的情况del_index=[del_index,max(strfind(s1(1:ain_index(i)),' '))+1:ain_index(i)+2];elseif s1(ain_index(i)+3)=='"' %单词用""括起来的情况del_index=[del_index,max(strfind(s1(1:ain_index(i)),' '))+2:ain_index(i)+2];endends1(del_index)=[];s2=s1;end

测试

>> s1 = 'I had to explain that "ain" is not a word';
s2 = 'I had to  that "" is not a word';remAin(s1)
>> strcmp(ans,s2)ans =logical1
>> s1 = 'The pain from my migraine makes me complain';
s2 = 'The  from my migraine makes me ';remAin(s1)ans ='The  from my migraine makes me '
>>  strcmp(ans,s2)ans =logical1

其它优秀代码

function s2 = remAin(s1)%% Split stringsstring_split = strsplit(s1,' ');%% Replace words which ends with 'ain' to emptyx = regexprep(string_split,'\w*ain$','');%% Convert separate cells into strings2 = strjoin(x);%% Find exception: when ain is inside apostrophe[~,startIndex,endIndex] = regexp(s2, '(?<=")[^"]+(?=")', 'match');s2(startIndex:endIndex) = [];end

http://www.ppmy.cn/news/598714.html

相关文章

IDEA报错ain] o.s.b.d.LoggingFailureAnalysisReporter

IDEA报错ain] o.s.b.d.LoggingFailureAnalysisReporter : 详细描述 An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.isOv…

adv7180 驱动 设置ain 输入

https://github.com/analogdevicesinc/linux/blob/adv7280/drivers/media/i2c/adv7180.c

CC2540 ADC实验总结(使用AIN0)

本实验基于Blue博文与部分代码。 http://www.cnblogs.com/BlueMountain-HaggenDazs/p/4298381.html 没有测试原博主的代码&#xff0c;但原博主的代码在我这里使用AIN0通道时读取数据有错误&#xff0c;原因是原博主忽视了ADCH的符号位&#xff08;因ADCL与ACDH合起来为一个sig…

Ain_EditPlus配置安装教程

1&#xff09;首先可以百度EditPlus进行下载或者在官网https://www.editplus.com/download.html进行下载&#xff08;英文版&#xff09; 2&#xff09;下载EditPlus汉化包进行汉化 3&#xff09;安装时&#xff0c;百度&#xff1a;EditPlus注册机或者http://www.jb51.net/too…

redis源码之:扩容后的dictScan遍历顺序与JDK的hashMap扩容机制

进入正题前&#xff0c;先来复习下关于2次幂的mod运算 设n为2次幂&#xff0c;数a mod n 等价于 a & n-1 从二进制来看&#xff0c;相当于余数为a省去n最高位左侧的所有位(含最高位)&#xff0c;保留n右侧所有低位即为余数 如&#xff1a;a 7(0000_0111),n4(0000_0100),通…

6月22日每日两题

第一题:统计数字 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。 输入格式: 第1行是整数n(1<=n<=200000),表示自然数的个数。…

FPS - 第一人称射击游戏

第一人称射击类游戏,FPS(First-person shooting game), 严格来说第一人称射击游戏属于ACT类游戏的一个分支&#xff0c;但和RTS类游戏一样&#xff0c;由于其在世界上的迅速风靡&#xff0c;使之发展成了一个单独的类型。 FPS(First-person Shooting)第一人称视角射击游戏顾名…

游戏类别整理

RPG&#xff1a;角色扮演游戏&#xff08;Role-playing game&#xff09;。 《口袋妖怪》&#xff1a;由Game Freak和Creatures株式会社开发&#xff0c;由任天堂发行的一系列游戏 《战神3》 对应&#xff1a;主机端-动作RPG类 MMORPG&#xff1a;是英文Massive&#xff08…