大小写互换

大小写互换

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 0
描述
      现在给出了一个只包含大小写字母的字符串,不含空格和换行,要求把其中的大写换成小写,小写换成大写,然后输出互换后的字符串。
输入
第一行只有一个整数m(m<=10),表示测试数据组数。
接下来的m行,每行有一个字符串(长度不超过100)。
输出
输出互换后的字符串,每组输出占一行。
样例输入
2
Acm
ACCEPTED
样例输出
aCM
accepted
来源
原创
上传者

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值