Back to Leetcode Go

709. To Lower Case

website/content.en/ChapterFour/0700~0799/0709.To-Lower-Case.md

1.7.97960 B
Original Source

709. To Lower Case

Problem

Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.

Example 1:

Input: s = "Hello"
Output: "hello"

Example 2:

Input: s = "here"
Output: "here"

Example 3:

Input: s = "LOVELY"
Output: "lovely"

Constraints:

  • 1 <= s.length <= 100
  • s consists of printable ASCII characters.

Problem Statement

Given a string s, convert the uppercase letters in the string to the same lowercase letters and return the new string.

Solution

  • Easy problem: convert the uppercase letters in the string to lowercase letters.

Code

go
func toLowerCase(s string) string {
    runes := [] rune(s)
    diff := 'a' - 'A'
    for i := 0; i < len(s); i++ {
        if runes[i] >= 'A' && runes[i] <= 'Z' {
            runes[i] += diff
        }
    }
    return string(runes)
}