输入字符,如果是字母则汇报并返回字母在字母表中序号的程序
输入字符,如果是字母则报告并返回字母在字母表中序号的程序
出错,感觉没有进到循环里去。不知道哪里有问题。
------解决方案--------------------
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;
}
------解决方案--------------------
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;
}