大小写互换
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
0
-
描述
-
现在给出了一个只包含大小写字母的字符串,不含空格和换行,要求把其中的大写换成小写,小写换成大写,然后输出互换后的字符串。
-
输入
-
第一行只有一个整数m(m<=10),表示测试数据组数。
接下来的m行,每行有一个字符串(长度不超过100)。
输出
- 输出互换后的字符串,每组输出占一行。 样例输入
-
2 Acm ACCEPTED
样例输出
-
aCM accepted
来源
- 原创 上传者
代码:
优秀代码:#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <algorithm> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int n; cin>>n; getchar(); while(n--){ char input[101]; gets(input); int length=strlen(input); for(int i=0;i<length;i++){ printf("%c",(input[i]>=95)?(input[i]-32):(input[i]+32)); } printf("\n"); } return 0; }
01.
#include<stdio.h>
02.
int
main()
03.
{
04.
int
a,b,c,n;
05.
char
x;
06.
scanf
(
"%d"
,&n);
07.
getchar
();
08.
while
(n--)
09.
{
10.
while
(
scanf
(
"%c"
,&x)&&x!=
'\n'
)
11.
{
12.
if
(x>=97&&x<=122)
printf
(
"%c"
,x-32);
13.
else
if
(x<=90&&x>=64)
printf
(
"%c"
,x+32);
14.
}
15.
printf
(
"\n"
);
16.
}
17.
}
对比分析:优秀代码是一个字符一个字符的进行处理,这样一方面节省了直接将一行存放在一个字符数组中所用的空间,另一方面也节省了求字符串长度的时间。
但我改了没效果,时间和大小和没改一样啊。
int main(int argc, char** argv) { int n; cin>>n; getchar(); while(n--){ char c=getchar(); while(c != '\n'){ printf("%c",(c>=95)?(c-32):(c+32)); c=getchar(); } printf("\n"); } return 0; }
-
第一行只有一个整数m(m<=10),表示测试数据组数。