DEV Community

Cover image for Algorithms Problem Solving: to Lower case
TK
TK

Posted on • Edited on • Originally published at leandrotk.github.io

1 1

Algorithms Problem Solving: to Lower case

This post is part of the Algorithms Problem Solving series.

Problem description

This is the To Lower Case problem. The description looks like this:

Implement function ToLowerCase() that has a string parameter str,
and returns the same string in lowercase.

Examples

Example 1:
Input: "Hello"
Output: "hello"

Example 2:
Input: "here"
Output: "here"

Example 3:
Input: "LOVELY"
Output: "lovely"
Enter fullscreen mode Exit fullscreen mode

Solution

My solution to this problem was to iterate through the entire string, and for each character, I append its lower case version. To verify if it needs to transform the character to lower case, I simply get the integer representation of the Unicode character by using the ord function. If it is between 65 and 90, both inclusive, the character is capitalized, so I need to transform it to lower case.

To transform to lower case, I just need to:

  • Transform to the Unicode integer
  • Sum 32
  • Transform back to the character representation of the Unicode

After the process of each character, I have my list of lower case characters. Then, I just need to join all the characters to get my final string back.

def lower_case_char(char):
    if ord(char) >= 65 and ord(char) <= 90:
        return chr(ord(char) + 32)

    return char

def lower_case(str):
    list_of_chars = []

    for char in str:
        list_of_chars.append(lower_case_char(char))

    return ''.join(list_of_chars)
Enter fullscreen mode Exit fullscreen mode

Resources

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay