算法训练 大小写转换

来源:互联网 发布:数据库pdforacle网盘 编辑:程序博客网 时间:2024/05/01 09:17

问题描述
  编写一个程序,输入一个字符串(长度不超过20),然后把这个字符串内的每一个字符进行大小写变换,即将大写字母变成小写,小写字母变成大写,然后把这个新的字符串输出。
  输入格式:输入一个字符串,而且这个字符串当中只包含英文字母,不包含其他类型的字符,也没有空格。
  输出格式:输出经过转换后的字符串。
输入输出样例
样例输入
AeDb
样例输出
aEdB

import java.util.Scanner;public class Main {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in = new Scanner(System.in);        StringBuffer str = new StringBuffer(in.next());        for ( int i = 0 ; i < str.length() ; i++){            char[] tmp = new char[1];            tmp[0] = str.charAt(i);            if ( tmp[0] >= 'A' && tmp[0] <= 'Z'){                 tmp[0] += 32;                str.replace(i, i+1, new String(tmp));            }else if ( tmp[0] >= 'a' && tmp[0] <= 'z'){                 tmp[0] -= 32;                str.replace(i, i+1, new String(tmp));            }        }        System.out.print(str.toString());        in.close();    }}

这里写图片描述

1 0