To Lower Case leetcode solution in c++

Sure! Here is a solution in C++ to convert a string to lowercase:

#include <iostream>
#include <string>

std::string toLowerCase(std::string str) {
    for (int i = 0; i < str.length(); i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
    return str;

int main() {
    std::string input = "Hello World";
    std::string lowerCaseString = toLowerCase(input);
    std::cout << lowerCaseString << std::endl;
    return 0;


  • The function toLowerCase takes a string str as input and returns a modified version of the string where all uppercase letters are converted to lowercase.
  • In the toLowerCase function, we use a loop to iterate over each character in the string.
  • For each character, we check if it is an uppercase letter by comparing its ASCII value with the range of uppercase letters ('A' to 'Z').
  • If the character is an uppercase letter, we convert it to lowercase by adding 32 to its ASCII value. This works because the ASCII values of uppercase letters are consecutive and the ASCII values of lowercase letters are also consecutive.
  • Finally, we return the modified string.
  • In the main function, we demonstrate the usage of the toLowerCase function by converting the string "Hello World" to lowercase and printing the result.

I hope this explanation helps! Let me know if you have any further questions.