编写一个函数RegularPlural,其功能是实现一个英文单词的复数形式。复数的规则为:
(1) 如果单词末尾为s,x,z,ch或sh,则在后面加es
(2) 如果单词末尾为y,且前一个字母为辅音(除a, e, i, o, u以外的其它情况),则把y改成ies。
(3) 如果是其它情形,一律在后面加s。
编写测试程序,输入一个长度小于20的单词,输出该单词的复数形式。
输入:
box
输出:
boxes
(用C语言解决)
一个实现:
#include <stdio.h>
#include <string.h>
void RegularPlural(char * str){
int lens = strlen(str);
char ch = str[lens-1];
char pch = str[lens-2];
//第一种情况
if((ch=='s')||(ch=='x')||(ch=='z')
||(ch=='h'&&pch=='c')||(ch=='h'&&pch=='s')){
str[lens]='e';
str[lens+1]='s';
str[lens+2]='\0';
}else if((ch=='y')&&(pch!='a'&&pch!='e'&&pch!='i'&&pch!='o'&&pch!='u')){ //第二种情况
str[lens-1]='i';
str[lens]='e';
str[lens+1]='s';
str[lens+2]='\0';
} else { //其他
str[lens] = 's';
str[lens+1]='\0';
}
}
int main(void){
char word[30];
//循环测试代码
while(1){
gets(word);
RegularPlural(word);
printf("%s\n",word);
}
}