先附上ASCII码图
不难发现,小写字母与大写字母相差32
小写转大写程序如下:
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
cin>>c;
c = c - 32;
cout<<c;
return 0;
}
运行结果:
输入:a
输出:A
大写转小写程序:
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
cin>>c;
c = c + 32;
cout<<c;
return 0;
}
运行结果:
输入:A
输出:a
自动转换程序:
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
cin>>c;
//判断大小写,转换
if(c >= 'A' && c <= 'Z'){
c = c + 32; //大写转小写
}
else{
c = c - 32; //小写转大写
}
cout<<c; //输出
return 0;
}
洛谷题目:URL