#include <stdio.h>
int main()
{
char ch = 0;
scanf_s("%c", &ch);
if (ch >= 'a' && ch <= 'z')
printf("%c\n", ch - 32);
else
printf("%c\n", ch + 32);
return 0;
}
或可以写为
#include <stdio.h>
int main()
{
char ch = 0;
while (scanf_s("%c", &ch) == 1)//如果接收到一个字符返回1
{
if (ch >= 'a' && ch <= 'z')
printf("%c\n", ch - 32);
else if (ch >= 'A' && ch <= 'Z')
printf("%c\n", ch + 32);
}
return 0;
}
//其实库函数中有判断大小写的:函数islower()判小写,isupper()判大写
//库函数中也有转大小写函数:函数toupper()转大写,tolower()转小写 }所需头文件是#include <ctype.h>
//所以也可以写成:
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch = 0;
while (scanf_s("%c", &ch) != EOF)
{
if (islower(ch))
printf("%c\n", toupper(ch));
else if (isupper(ch))
printf("%c\n", tolower(ch));
}
return 0;
}