蔚来科技 安全团队:从键盘上输入一个英文字母,若是小写则输出大写,若是大写则输入对应的小写,其他字符为非法字符

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 12:39:39
请用简单一点的c++

#include <iostream.h>
#include <cctype>
int main()
{
char ch;
cin >> ch;
if(!isalpha(ch))/*测试是否为大小写字母*/
cout << "invalid" << endl;
else
{

if(islower(ch))
ch = 'A' + ch - 'a';
else if (isupper(ch))
ch = 'a' + ch - 'A';
cout << ch << endl;
}
return 0;
}
在程序中要尽量的使用is系列的函数,因为有的编码可能和ASCII不一样,这样,用一些其他的方法就有可能出错。

#include<iostream.h>

char lower(char c){
return(c+32);
}

char upper(char c){
return(c-32);
}

int main(){
char c;
int flag=1;
cin>>c;
if(c>='a'&&c<='z') {cout<<upper(c);flag=0;}
if(c>='A'&&c<='Z') {cout<<lower(c);flag=0;}
if(flag) cout<<"invalid";
}

#include <stdio.h>
#include <ctype.h>
void main()
{
char ch;
ch=getchar();
if(isalnum(ch)!=0)//测试ch是否为大、小写字母或数字
{
if(islower(ch)==1)ch=ch-32;//小->大
else if(isupper(ch)==1)ch=ch+32;//大->小
else break;//是数字不处理
}
else printf("非法字符/n");

}