Sunday, February 21, 2021

Leetcode Everyday: 709. To Lower Case. Easy

It's in the rules not to use the built-in method. Here's a solution without using the built-in method
public class Solution {
    public string ToLowerCase(string str) {
        var sb = new StringBuilder(str);
        for (var i = 0; i < sb.Length; ++i) {
            char c = sb[i];
            if (c >= 'A' && c <= 'Z') {
                sb[i] =  (char)(c + ('a' - 'A'));
            }
        }
        return sb.ToString();
    }
}
Source: https://leetcode.com/problems/to-lower-case/

No comments:

Post a Comment