Open In App

Write a program to convert Uppercase to LowerCase

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Write a program to convert Uppercase to LowerCase

Examples:

Input: str= “GEEKSFORGEEKS”
Output: “geeksforgeeks”

Input: str= “CAT”
Output: “cat”

Approach:

Iterate through each character in a string and add 32 in the ASCII value of each character.

  • ASCII value of lowercase char ‘a’ to ‘z’ ranges from 97 to 122
  • ASCII value of uppercase char ‘A’ to ‘Z’ ranges from 65 to 92
  • For conversion add 32 in the ASCII value of input char

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
int main() {
 
    string str="GEEKSFORGEEKS";
    string ans="";
 
    for(int i=0;i<str.size();i++){
    ans+=str[i] +32 ;
    }
     
    cout<<ans;
}


Java




import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        // Input string
        String str = "GEEKSFORGEEKS";
         
        // Resultant string
        String ans = "";
   
        // Iterate through each character in the input string
        for (int i = 0; i < str.length(); i++) {
            // Convert the character to lowercase and append to the result string
            ans += Character.toLowerCase(str.charAt(i));
        }
     
        // Print the result
        System.out.println(ans);
    }
}


Python3




# Given string
str_input = "GEEKSFORGEEKS"
 
# Empty string to store the result
ans = ""
 
# Iterate through each character in the input string
for char in str_input:
    # Convert each character to lowercase and append it to the result string
    ans += char.lower()
 
# Print the result string
print(ans)


C#




using System;
 
class Program
{
    static void Main()
    {
        // Input string
        string str = "GEEKSFORGEEKS";
         
        // Resultant string
        string ans = "";
   
        // Iterate through each character in the input string
        for (int i = 0; i < str.Length; i++)
        {
            // Convert the character to lowercase and append to the result string
            ans += char.ToLower(str[i]);
        }
     
        // Print the result
        Console.WriteLine(ans);
    }
}


Javascript




// JavaScript code for above approach
// The code converts all characters in the input string to lowercase.
 
// Input string
let str = "GEEKSFORGEEKS";
 
// Resultant string
let ans = "";
 
// Iterate through each character in the input string
for (let i = 0; i < str.length; i++) {
    // Convert the character to lowercase and append to the result string
    ans += str[i].toLowerCase();
}
 
// Print the result
console.log(ans);


Output

geeksforgeeks

Time Complexity: O(N)
Auxiliary space: O(1)

Related Article:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads