您的位置:首页 > 其它

C primer plus 第九章 练习6:

2016-05-30 12:07 218 查看
/*
============================================================
编写一个程序,使其从标准输入读取字符,知道遇到文件结尾,
对于每个字符,程序需要检查并报告该字符是否是一个字母。如果
是的话,程序还应报告该字母在字母表中的数值位置。例如,c和C
的字母位置都是3.可以先实现这样一个函数:接受一个字符参数,
如果该字符为字母则返回字母的数值位置,否则返回-1。
============================================================
*/

#include <stdio.h>
#include <string.h>
int fan(char x);
int main(void)
{
char x;

while((scanf("%c", &x) == 1) && x != '0')
{
fan(x);
fflush(stdin);
}
return 0;
}

int fan(char x)
{
int i;

if((x >= 65 && x <= 90)||(x >= 97 && x <= 122))
{
if(x >= 65 && x <= 90)
printf("%c, it's number is %d.\n", x, x-64);
else if(x >= 97 && x <= 122)
printf("%c, it's number is %d.\n", x, x-96);
else
printf("%c, it's number is -1.\n", x);
}
return 0;
}


以上程序的瑕疵在于:无法对字符串进行输出。

网络上的程序均是如此,之后对其进行改进。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: