To Lower Case

Updated on 27 June, 2025
To Lower Case header image

Problem Statement

In this problem, we are given a string s that can contain both uppercase and lowercase letters among other printable ASCII characters. The task is to transform all uppercase letters in the string to their corresponding lowercase versions. For characters in the string that are already lowercase or aren't alphabetic (such as numbers or punctuation marks), they should remain unchanged. The function needs to return the new string where every uppercase letter has been replaced with its lowercase counterpart.

Examples

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.

Approach and Intuition

The problem requires converting all uppercase letters in a string to their lowercase equivalents and is a straightforward process:

  1. Read the input string s which is guaranteed by the constraints to be at least 1 character long and no more than 100 characters, and consists of printable ASCII characters.

  2. Traverse each character in the string:

    • If the character is an uppercase letter (from 'A' to 'Z'), convert it to its corresponding lowercase letter (from 'a' to 'z').
    • If the character is not an uppercase letter, it remains unchanged.
  3. Construct a new string with these updated characters.

  4. Return the transformed string.

This approach can be implemented efficiently within the given constraints and with the simplicity of the task, the conversion can be done in a single pass through the string, ensuring a time complexity of O(n), where n is the length of the string. By ensuring each character is checked and potentially transformed once, we achieve a direct and optimal solution. This process leverages the basic ASCII value conversions or string manipulation functions provided in most programming languages, making the implementation straightforward.

Solutions

  • Java
java
class Solution {
    public boolean checkUpperCase(char c) {
        return 'A' <= c && c <= 'Z';
    }

    public char convertToLower(char c) {
        return (char) ((int)c | 32);
    }

    public String convertToLowerCase(String str) {
        StringBuilder result = new StringBuilder();
        for (char ch : str.toCharArray()) {
            result.append(checkUpperCase(ch) ? convertToLower(ch) : ch);
        }
        return result.toString();
    }
}

This Java solution employs a class named Solution to convert a string to lowercase manually without using the standard library method toLowerCase().

  • The method checkUpperCase(char c) checks if the given character c is uppercase by comparing its ASCII value within the range of 'A' to 'Z'.
  • The convertToLower(char c) method transforms an uppercase character into its lowercase counterpart by using bitwise OR operation to add 32 to its ASCII value. This changes the case as the ASCII difference between uppercase and lowercase characters is 32.
  • The convertToLowerCase(String str) is the main method that takes a string and constructs a new lowercase string. It iterates over each character of the input string:
    • If the character is uppercase (as checked by checkUpperCase(char)), it converts it using the convertToLower(char) method.
    • If the character is not uppercase, it is appended to the StringBuilder without changes.
  • Finally, this constructed string is returned.

By following the outlined strategy, this code converts all uppercase letters in the input string to their lowercase forms, preserving any non-alphabet characters and lowercase letters as they are. This approach is efficient and showcases an alternative method to string manipulation techniques typical in Java.

Comments

No comments yet.