2017年C语言期末考试
Problem A: 编写函数:第几个英文字母 (Append Code)
Description
输入一个英文字母,大小写均可,是第几个英文字母?
编写两个函数chseq()、output(),完成程序,其原型为:
int chseq(char ch);
功能:形参ch传入一个英文字母,大小写均可。用int类型返回其顺序。
Output(char ch, int n);
功能:输出字母ch是第几个英文字母。
函数的调用格式见“Append Code”。
Input
输入为一个英文字母ch,大小写均有可能。
Output
输出为:“ch is ??? character.”。其中“???”表示序数(第几个)。
在序数k(第k个)中:个位数为1的是“st”结尾,为2的是“nd”结尾,为3的是“rd”结尾,为4~9和0的都是“th”结尾。
例如:“a is 1st character.”、”B is 2nd character.”等等。
Sample Input
C
Sample Output
C is 3rd character.
HINT
Append Code
int main()
{
char ch = getchar();
int n = chseq(ch);
output(ch, n);
}
ans:
#include<stdio.h>
#include<string.h>
int chseq(char ch)
{
if(ch>='a'&&ch<='z'){
return (ch-'a'+1);
}else if(ch>='A'&&ch<='Z'){
return (ch-'A'+1);
}
return 0;
}
void output(char ch, int n)
{
if(n%10==1){
printf("%c is %dst character.\n",ch,n);
}else if(n%10==2){
printf("%c is %dnd character.\n",ch,n);
}else if(n%10==3){
printf("%c is %drd character.\n",ch,n);
}else{
printf("%c is %dth character.\n",ch,n);
}
}
int main()
{
char ch = getchar();
int n = chseq(ch);
output(ch, n);
return 0;
}