输入字符,如果是字母则报告并返回字母在字母表中序号的程序

freelywinder 2014-07-11 08:23:37
出错,感觉没有进到循环里去。不知道哪里有问题。
#include<stdio.h>
int if_char();
void main()
{
int i;

printf("please enter something,if there is a letter,\n");
printf("i'll report it and its position in the alphabet.\n");
printf("enter a '&' to end this programme.\n");
while((i=if_char())!=0) //如果是&则结束
{
if(i>0) //如果是字母则显示其在字母表中的位置
printf(" and it's %d in the alphabet.\n",i);
}
printf("Bye!\n");
system("pause");
}

int if_char()
{
int ch;

ch=getchar(); //获取字符
if(ch='&') // 如果是结束符号&则返回0
return 0;
else if((ch>=65&&ch<=90)||(ch>=97&&ch<=122)) //如果是字母则进入字母大小写判别
{
if(ch>=65&&ch<=90) //如果是小写则返回字母ASCII码减64,(a返回1)
{
printf("%c is a letter",ch);
return ch-64;
}
else //如果是大写则返回字母ASCII码减96,(A返回1)
{
printf("%c is a letter",ch);
return ch-96;
}
}
else //非字母返回-1
return -1;
}
...全文
385 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
freelywinder 2014-07-12
  • 打赏
  • 举报
回复
谢谢2L,找了半天都发现这个问题。
707wk 2014-07-11
  • 打赏
  • 举报
回复
lz把==和=搞混了
#include <stdio.h>

int if_char();

void main()
{
    int i; 
    printf("please enter something,if there is a letter,\n");
    printf("i'll report it and its position in the alphabet.\n");
    printf("enter a '&' to end this programme.\n");
    while((i=if_char())!=0)                          //如果是&则结束
    {
        if(i>0)                            //如果是字母则显示其在字母表中的位置
            printf(" and it's %d in the alphabet.\n",i);
    }
    printf("Bye!\n");
    system("pause");
}
 
int if_char()
{
    int ch;
    ch=getchar();            //获取字符
    if(ch=='&')              // 如果是结束符号&则返回0
        return 0;
    else if((ch>=65&&ch<=90)||(ch>=97&&ch<=122))   //如果是字母则进入字母大小写判别
    {
        if(ch>=65&&ch<=90)        //如果是小写则返回字母ASCII码减64,(a返回1)
        {
            printf("%c is a letter",ch);
            return ch-64;
        }
        else                 //如果是大写则返回字母ASCII码减96,(A返回1)
        {
            printf("%c is a letter",ch);
            return ch-96;
        }
    }
    else                                             //非字母返回-1
        return -1;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧