Given a string s, convert all uppercase characters in the string to their corresponding lowercase characters and return the resulting string.
Examples:
Input: s = "ABCddE"
Output: "abcdde"
Explanation: A, B, C and E are converted to a, b, c and e thus all uppercase characters of the string converted to lowercase letter.Input: s = "LMNOppQQ"
Output: "lmnoppqq"
Explanation: L, M, N, O, and Q are converted to l, m, n, o and q thus all uppercase characters of the string converted to lowercase letter.
Table of Content
Using Inbuilt Function - O(n) Time and O(1) Space
Most programming languages provide a built-in function to convert characters or strings to lowercase. The idea is to use this function to transform every uppercase character into its lowercase equivalent.
- Traverse the string.
- Convert each character to lowercase using the built-in function.
- Return the modified string.
#include <bits/stdc++.h>
using namespace std;
// Function to convert a string to lowercase
string toLower(string& s)
{
// Convert the entire string to lowercase
transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}
int main()
{
string s = "GeEkS FoR GeEkS";
cout << toLower(s) << endl;
return 0;
}
class GFG {
// Function to convert a string to lowercase
static String toLower(String s)
{
// Convert the entire string to lowercase
return s.toLowerCase();
}
public static void main(String[] args)
{
String s = "GeEkS FoR GeEkS";
System.out.println(toLower(s));
}
}
# Function to convert a string to lowercase
def toLower(s):
# Return the lowercase version of the string
return s.lower()
# Driver Code
if __name__ == "__main__":
s = "GeEkS FoR GeEkS"
print(toLower(s))
using System;
class GFG {
// Function to convert a string to lowercase
static string toLower(string s)
{
// Convert the entire string to lowercase
return s.ToLower();
}
static void Main()
{
string s = "GeEkS FoR GeEkS";
Console.WriteLine(toLower(s));
}
}
// Function to convert a string to lowercase
function toLower(s)
{
// Convert the entire string to lowercase
return s.toLowerCase();
}
// Driver Code
let s = "GeEkS FoR GeEkS";
console.log(toLower(s));
Output
geeks for geeks
Converting Characters Manually - O(n) Time and O(1) Space
The ASCII values of uppercase and lowercase English letters differ by 32.
- 'A' = 65, 'a' = 97
- 'B' = 66, 'b' = 98
Thus, for every uppercase letter, adding 32 converts it to its lowercase counterpart.
- Traverse the string character by character.
- If the current character lies between 'A' and 'Z', add ('a' - 'A') (i.e., 32) to it.
- Leave all other characters unchanged.
- Return the modified string.
#include <bits/stdc++.h>
using namespace std;
// Function to convert a string to lowercase
string toLower(string s)
{
// Traverse each character
for (char &ch : s)
{
// Check if the current character is an
// uppercase letter
if (ch >= 'A' && ch <= 'Z')
{
// Convert it to lowercase
ch = ch + ('a' - 'A');
}
}
return s;
}
int main()
{
string s = "GeEkS FoR GeEkS";
cout << toLower(s) << endl;
return 0;
}
class GFG {
// Function to convert a string to lowercase
static String toLower(String s)
{
// Convert the string into a character array
char[] arr = s.toCharArray();
// Traverse each character
for (int i = 0; i < arr.length; i++) {
// Check if the current character is an
// uppercase letter
if (arr[i] >= 'A' && arr[i] <= 'Z') {
// Convert it to lowercase
arr[i] = (char)(arr[i] + ('a' - 'A'));
}
}
// Convert the character array back to a string
return new String(arr);
}
public static void main(String[] args)
{
String s = "GeEkS FoR GeEkS";
System.out.println(toLower(s));
}
}
# Function to convert a string to lowercase
def toLower(s):
# Convert the string into a list of characters
s = list(s)
# Traverse each character
for i in range(len(s)):
# Check if the current character is an uppercase letter
if 'A' <= s[i] <= 'Z':
# Convert it to lowercase
s[i] = chr(ord(s[i]) + (ord('a') - ord('A')))
# Convert the character list back to a string
return "".join(s)
# Driver Code
if __name__ == "__main__":
s = "GeEkS FoR GeEkS"
print(toLower(s))
using System;
class GFG {
// Function to convert a string to lowercase
static string toLower(string s)
{
// Convert the string into a character array
char[] arr = s.ToCharArray();
// Traverse each character
for (int i = 0; i < arr.Length; i++) {
// Check if the current character is an
// uppercase letter
if (arr[i] >= 'A' && arr[i] <= 'Z') {
// Convert it to lowercase
arr[i] = (char)(arr[i] + ('a' - 'A'));
}
}
// Convert the character array back to a string
return new string(arr);
}
static void Main()
{
string s = "GeEkS FoR GeEkS";
Console.WriteLine(toLower(s));
}
}
// Function to convert a string to lowercase
function toLower(s)
{
// Convert the string into an array of characters
let arr = s.split("");
// Traverse each character
for (let i = 0; i < arr.length; i++) {
// Check if the current character is an uppercase
// letter
if (arr[i] >= "A" && arr[i] <= "Z") {
// Convert it to lowercase
arr[i] = String.fromCharCode(
arr[i].charCodeAt(0)
+ ("a".charCodeAt(0) - "A".charCodeAt(0)));
}
}
// Convert the character array back to a string
return arr.join("");
}
// Driver Code
let s = "GeEkS FoR GeEkS";
console.log(toLower(s));
Output
geeks for geeks