Convert lower case to upper case or upper case to lower case• Write a program that inputs a character using ASCII table to convert lower case to upper case or upper case to lower case• Input a character repeatedly using the While statement• Hint1 : fflush(stdin);• Use the getchar() and putchar()• Use the ctype.h and functions associated with ctype.h
int main()
{
char a;
while(true)
{
printf("Input Alphabat :");
a = getchar();
getchar();
if(a>='a' && a<='z')
printf("Result : %c\n",a-32);
else if(a>='A' && a<='Z')
printf("Result : %c\n",a+32);
else
printf("Wrong intput ! try again\n");
}
return 0;
}
#include <stdio.h>
#include <ctype.h>
int main()
{
char a;
while(1)
{
printf("Input Alphabat :");
a = getchar();
getchar();
if(islower(a)){
printf("Result : ");
putchar(toupper(a));
putchar('\n');
}else if(isupper(a)){
printf("Result : ");
putchar(tolower(a));
putchar('\n');
}else{
printf("Wrong intput ! try again\n");
}
}
return 0;
}
刚刚回答的有问题,未看到需要使用fflush(stdin); 现在补上程序,fflush(stdin);作用是清空输入缓冲区,可以防止之前的输入造成干扰。
但是从当前这个程序的测试情况来看,不使用也没有什么问题。
#include <stdio.h>
#include <ctype.h>
int main()
{
char a;
while(1)
{
printf("Input Alphabat :");
fflush(stdin);
a = getchar();
getchar();
if(islower(a)){
printf("Result : ");
putchar(toupper(a));
putchar('\n');
}else if(isupper(a)){
printf("Result : ");
putchar(tolower(a));
putchar('\n');
}else{
printf("Wrong intput ! try again\n");
}
}
return 0;
}