大小写互换
来源:互联网 发布:苹果手机无线抢网络 编辑:程序博客网 时间:2024/06/08 05:31
大小写互换
时间限制:1000 ms | 内存限制:65535 KB
难度:0
- 描述
- 现在给出了一个只包含大小写字母的字符串,不含空格和换行,要求把其中的大写换成小写,小写换成大写,然后输出互换后的字符串。
- 输入
- 第一行只有一个整数m(m<=10),表示测试数据组数。
接下来的m行,每行有一个字符串(长度不超过100)。 - 输出
- 输出互换后的字符串,每组输出占一行。
- 样例输入
2AcmACCEPTED
- 样例输出
aCMaccepted
- 来源
- 原创
- 上传者
ACM_杨延玺
代码:
#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;}
阅读全文
0 0
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写互换
- 大小写字母互换
- 南阳理工:大小写互换
- 字符串大小写互换
- NYOJ 457 大小写互换
- 水:大小写互换
- NYOJ457大小写互换
- java基础知识的总结
- linux解决Tomcat内存溢出
- 如何将CString转换为LPCSTR
- javamail发送邮件
- tracert工作原理
- 大小写互换
- HDU1045 (3/600)
- WebView简介(联网权限)
- 第20篇.ssh远程登录
- jquery操作select(取值,设置选中)
- 工作小结
- lintcode(450)K组翻转链表
- spring+quartz实现定时任务
- Unity3d网格合并