大文字/小文字の変換
- 文字列を大文字/小文字へ変換
- toUpperCase, toLowerCaseメソッド
- 大文字/小文字かどうかを判別
- isUpperCase. isLowerCaseメソッド
public class Main {
public static void main(String[] args){
String str1 = new String("MoHuNeKo");
String upper_str1 = str1.toUpperCase();
String lower_str1 = str1.toLowerCase();
System.out.println("入力文字列 : " + str1);
System.out.println("1文字目は大文字? : " + Character.isUpperCase(str1.charAt(0)));
System.out.println("大文字変換 : " + upper_str1);
System.out.println("小文字変換 : " + lower_str1);
System.out.println("=======^・ω・^=======");
String str2 = new String("The current world population is about 7,594,690,000!");
String upper_str2 = str2.toUpperCase();
String lower_str2 = str2.toLowerCase();
System.out.println("入力文字列 : " + str2);
System.out.println("2文字目は小文字? : " + Character.isLowerCase(str2.charAt(1)));
System.out.println("大文字変換 : " + upper_str2);
System.out.println("小文字変換 : " + lower_str2);
System.out.println("==============");
System.out.println("大文字? : " + Character.isUpperCase('\n'));
System.out.println("小文字? : " +Character.isLowerCase('\t'));
}
}
入力文字列 : MoHuNeKo
1文字目は大文字? : true
大文字変換 : MOHUNEKO
小文字変換 : mohuneko
=======^・ω・^=======
入力文字列 : The current world population is about 7,594,690,000!
2文字目は小文字? : true
大文字変換 : THE CURRENT WORLD POPULATION IS ABOUT 7,594,690,000!
小文字変換 : the current world population is about 7,594,690,000!
==============
大文字? : false
小文字? : false
大文字と小文字の入れ替え(ITP1-8)
与えられた文字列の小文字と大文字を入れ替えるプログラムを作成してください。
Input
文字列が1行に与えられます。
Output
与えられた文字列の小文字と大文字を入れ替えた文字列を出力して下さい。アルファベット以外の文字はそのまま出力して下さい。
Constraints
- 入力される文字列の長さ < 1200
Sample Input
fAIR, LATER, OCCASIONALLY CLOUDY.
Sample Output
Fair, later, occasionally cloudy.
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < line.length() ; i++){
char now = line.charAt(i);
if(Character.isUpperCase(now)) sb.append(Character.toLowerCase(now));
else if(Character.isLowerCase(now)) sb.append(Character.toUpperCase(now));
else sb.append(now);
}
System.out.println(sb);
}
}