[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)

Uppercase / lowercase conversion

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("Input string: " + str1);
    System.out.println("The first letter is uppercase? : " + Character.isUpperCase(str1.charAt(0)));
    System.out.println("Uppercase conversion: " + upper_str1);
    System.out.println("Lowercase conversion: " + 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("Input string: " + str2);
    System.out.println("The second letter is lowercase? : " + Character.isLowerCase(str2.charAt(1)));
    System.out.println("Uppercase conversion: " + upper_str2);
    System.out.println("Lowercase conversion: " + lower_str2);
    System.out.println("==============");
    System.out.println("uppercase letter? : " + Character.isUpperCase('\n'));
    System.out.println("Lowercase? : " +Character.isLowerCase('\t'));
   }
}
Input string: MoHuNeKo
The first letter is uppercase? : true
Uppercase conversion: MOHUNEKO
Lowercase conversion: mohuneko
=======^・ Ω ・^=======
Input string: The current world population is about 7,594,690,000!
The second letter is lowercase? : true
Uppercase conversion: THE CURRENT WORLD POPULATION IS ABOUT 7,594,690,000!
Lowercase conversion: the current world population is about 7,594,690,000!
==============
uppercase letter? : false
Lowercase? : false

Swapping uppercase and lowercase letters (ITP1-8)

Create a program that swaps lowercase and uppercase letters in a given string. Input The string is given on one line. Output Please output the character string in which the lowercase letters and uppercase letters of the given character string are exchanged. Please output the characters other than the alphabet as they are. Constraints

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);
    }
}

Recommended Posts

[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Ruby How to convert between uppercase and lowercase
Convert Java org.w3c.dom.Document objects and XML strings
[java] Summary of how to handle character strings
<java> Read Zip file and convert directly to string
Convert Java enum enums and JSON to and from Jackson
How to convert A to a and a to A using AND and OR in Java
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
[Java] Handling of character strings (String class and StringBuilder class)
Convert Java Powerpoint to XPS
How to convert Java radix
[Java] Convert ArrayList to array
Java DateFormat lowercase vs uppercase
[Java] Comparison method of character strings and comparison method using regular expressions
[Java] How to convert a character string from String type to byte type
Differences in how to handle strings between Java and Perl
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Java] Remove whitespace from character strings
[Java] Convert 1-to-N List to Map
How to convert LocalDate and Timestamp
Convert iso-2022-jp character string to utf-8
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
[Android] Convert Map to JSON using GSON in Kotlin and Java
[Java] How to convert from String to Path type and get the path
[Android] Convert Android Java code to Kotlin
[Java] Convert array to ArrayList * Caution
How to concatenate strings in java
Convert all Android apps (Java) to Kotlin
Convert from java UTC time to JST time
Java implementation to create and solve mazes
[Java] How to output and write files!
[Java] Convert Object type null to String type
Convert SVG files to PNG files in Java
Convert an array of strings to numbers
[Java] Precautions when comparing character strings with character strings
[Java] Comparison of String type character strings
[Introduction to Java] Variable declarations and types
Java to C and C to Java in Android Studio
Convert JSON to TSV and TSV to JSON with Ruby
[Java] Corrective notes for "Invalid escape character" and "Cannot map to encoding MS932"
Kotlin functions and lambdas to send to Java developers
[Android] How to convert a character string to resourceId
[Java] How to get and output standard input
Try to link Ruby and Java with Dapr
Java upload and download notes to Azure storage
[Java] Convert DB code to code value using enum
How to convert a solidity contract to a Java contract class
[Swift] Convert ByteArray and HexString to each other
How to get and study java SE8 Gold
Java8 to start now ~ forEach and lambda expression ~
Convert Java nested beans to aaa.bbb [0] .ccc format
How to access Java Private methods and fields
[Java] Convert and import file values with OpenCSV
[Java] How to use Calendar class and Date class