Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1: Input: “Hello” Output: “hello”
Example 2: Input: “here” Output: “here”
Example 3: Input: “LOVELY” Output: “lovely”
class Solution { public String toLowerCase(String str) { return str.toLowerCase(); } }; 另一种:
toCharArray() 方法将字符串转换为字符数组。
class Solution { public String toLowerCase(String str) { String result=""; for(char c:str.toCharArray()){ if(Character.isUpperCase(c)){ result=result+(char)(c+32); }else{ result=result+c; } } return result; // return str.toLowerCase(); } } package com.company; import java.security.PublicKey; import java.util.Scanner; import java.util.*; public class ToLowerCase709 { public static String toLowerCase(String str) { return str.toLowerCase(); } public static String tolower_second(String str){ String result=""; //toCharArray:将字符窜转成字符数组 for(char c:str.toCharArray()){ //isUpperCase判断是否是大写 if(Character.isUpperCase(c)){ result=result+(char)(c+32); }else{ result=result+c; } } return result; } public static void main(String[] args) { while (true) { String str = ""; Scanner scanner = new Scanner(System.in); str = scanner.nextLine(); //System.out.println(str); //第一种方法:java内置的toLowerCase的方法转成小写 System.out.println(toLowerCase(str)); //第二种方法:如果是大写,就用ASCII 大写+32=对应的小写 System.out.println(tolower_second(str)); } } }