题目描述
输入一个英文字母(可能是大写,也可能是小写),输出该字母在字母表中的序号(’a’和’A’的序号为1)。
输入
输入只有一个英文字母。
输出
输出一个整数,表示该字母在字母表的序号,输出单独占一行。
样例输入
D
样例输出
4
#include<stdio.h>
int main()
{
char x;
int n;
scanf("%c",&x);
if(x>='a' && x<='z')
n=x-96;
if(x>='A' && x<='Z')
n=x-64;
printf("%d",n);
return 0;
}
字符 | ASCII码 |
---|---|
‘A’~‘Z’ | 65~90 |
‘a’~‘z’ | 97~122 |
‘0’~‘9’ | 48~57 |
从网上看到其他方法,更简便一些
#include <stdio.h>
int main() {
int index = 0;
char c = getchar();
if (c>='a' && c<='z') {
index = c - 'a' + 1;
}
else if (c>='A' && c<='Z') {
index = c - 'A' + 1;
}
printf("%d\n",index);
return 0;
}