Java练习 SDUT-2746_大小写转换

大小写转换

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

X现在要学习英文以及各种稀奇古怪的字符的了。现在他想把一串字符中的小写字母变成大写字符,大写字母变成小写字母,其他的保持不变。

Input

输入有多组。
每组输入一个字符串,长度不大于80,不包含空格。

Output

输出转换后的字符串

Sample Input

A*
B+

Sample Output

a*
b+

Hint

Source

zmx

大小写转化的升级版,敲完之后才反应过来可以直接在原字符串上操作就可以……
因为不知道在Java中大小写字符串的码值差多少,所以采取了比较暴力的方式……

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int i,num;
		String s;
		char a[] = new char[85];
		num = 0;
		while(cin.hasNextLine())
		{
			s = cin.nextLine();
			num = 0;
			for(i=0;i<s.length();i++)
			{
				if(s.charAt(i)>='a'&&s.charAt(i)<='z')
					a[num++] = (char) (s.charAt(i) + 'A' - 'a');
				else if(s.charAt(i)>='A'&&s.charAt(i)<='Z')
					a[num++] = (char) (s.charAt(i) + 'a' - 'A');
				else
					a[num++] = s.charAt(i);
			}
			for(i=0;i<num;i++)
				System.out.print(a[i]);
			System.out.println();
		}
		cin.close();
	}
}
posted @ 2018-09-27 08:11  洛沐辰  阅读(285)  评论(0)    收藏  举报