-27 大小写字母转换 (15分)
输入一行字符,以回车符 '\n' 结束,将其中的大写字母转换为相应的小写字母后输出,小写字母转换为相应的大写字母后输出,其他字符按原样输出。
方法一
#include<stdio.h>
int main()
{
int i;
int n;
char s[100]={0};
gets(s);
for(i=0;i<=99;i++){
if(s[i]>='A'&&s[i]<='Z')
s[i]=s[i]-'A'+'a';
else if(s[i]>='a'&&s[i]<='z')
s[i]=s[i]-'a'+'A';
else
s[i]=s[i];
printf("%c",s[i]);
if(s[i]=='\0')
break;
}
return 0;
}
方法二
#include<stdio.h>
int main()
{
char c, temp;
int i;
while ((c=getchar()) != '\n'){
if (c >= 'a' && c <= 'z')
{temp = c - 'a' + 'A';
putchar(temp);}
else if (c >= 'A' && c <= 'Z')
{temp = c - 'A' + 'a';
putchar(temp);}
else
{temp = c;
putchar(temp);
}
}
return 0;
}
方法三
#include<stdio.h>
int main()
{
char c;
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z')
c=c+'A'-'a';
else if(c>='A'&&c<='Z')
c=c-('A'-'a');
printf("%c",c);
}
return 0;
}