Python删除单词后缀er,ly,ing与C++代码对比

Python删除单词后缀er,ly,ing与C++代码对比

如果英文单词以er或者ly或者ing结尾,那么把抬缀删除。下面是Python实现代码:

s = input("请输入一个英文单词:")
x = list(s)
if s.endswith("er") or s.endswith("ly"):
    x.pop();x.pop()
elif s.endswith("ing"):
    [x.pop() for _ in range(3)]
print("".join(x))

下面是C++实现代码:

#include 〈cstdio>
#include 〈cstring>

int main()
{
	char s[201],b[201];
	int i,n,len;

	gets(s);
	
	len=strlen(s);

    if(s[len-2]=='e'&&s[len-1]=='r') s[len-2]='\0';
 
    if(s[len-2]=='l'&&s[len-1]=='y') s[len-2]='\0';   
 
    if(s[len-3]=='i'&&s[len-2]=='n'&&s[len-1]=='g') s[len-3]='\0';
        
    puts(s);
    
    return 0;

}
李兴球

李兴球的博客是Python创意编程原创博客