编写一个程序,使其从标准输入读取字符,直到遇到0字符(注意是0字符,不是数字), 对于每个字符,程序需要检查并报告字符是否是一个字母。如果是的话,程序还应该 报告该字母在字母表中的数值位置。例如,c和C的字母位置都是3。可以先实现这样一个函数:
接收一个字符参数,如果该字符为字母则返回该字母的数值位置,否则返回-1
接收一个字符参数,如果该字符为字母则返回该字母的数值位置,否则返回-1
#include <stdio.h>
int cloc(char c);
int main(){
char c;
int res;
printf("请输入一个英文字符:\n");
while(1){
c = getchar();
getchar();//enter也是一个字符
if(c == '0'){
break;
}
res = cloc(c);
if(res == -1){
printf("input error\n");
}
printf("the %c is a alphaber and the char is at the place of %d\n",c,res);
}
return 0;
}
int cloc(char c){
if(c<='z'&&c>='a'){
return (c-'a'+1);
}else if(c<='Z'&&c>='A'){
return (c-'A'+1);
}else
return -1;
}