[709] LeetCode. Converted to lowercase

Description Title: implementing a function toLowerCase (), the function takes a string argument str, and converts the string to uppercase to lowercase, and then returns the new string.

Problem-solving ideas: The first idea, we can directly use toLowerCase method, a string of all the letters to lowercase , change the string of the last return; second way is, first string into an array of characters, and then to traverse the array, if the character is uppercase to lowercase turn, converted by ASCII code.

Implementation:

class Solution { 
    public String toLowerCase(String str) {
        //思路一
        //str = str.toLowerCase();
        //return str;

        //思路二
        char[] s = str.toCharArray();
        for(int i = 0; i < str.length(); i++){
            if(s[i]>='A' && s[i]<='Z'){
                s[i] += 32;
            }
        }
        return new String(s);
    }
}

 

Published 62 original articles · won praise 8 · views 988

Guess you like

Origin blog.csdn.net/qq_43553062/article/details/103933283