调用字符串 swapcase 逐字符互换大写与小写字母。
OJ: noi_openjudge
题目 ID: ch0107-14
难度:入门
标签:字符串python
日期: 2026-07-30 23:01
题意
将字符串中的大写字母替换为小写、小写替换为大写,其他字符不变。
思路
swapcase() 一次完成大小写互换,非字母字符保持原样。
代码
Python代码
python
print(input().swapcase())C++代码
cpp
#include <cstdio>
#include <cstring>
char str[500];
int idx=0;
int main(){
char t;
while(1){
int ret = scanf("%c",&t);
if( t == '\n' || t == '\r' || ret == EOF)
break;
str[++idx] = t;
}
int i;
for (i=1;i<=idx;i++){
if( str[i] >='a' && str[i] <='z' ){
printf("%c",str[i]+'A'-'a');
}
else if( str[i] >='A' && str[i] <='Z' ) {
printf("%c",str[i]+'a'-'A');
}
else
printf("%c",str[i]);
}
return 0;
}复杂度
时间复杂度和输出空间均为
总结
互换大小写与单向转换不同,应使用 swapcase。