Write a program to convert Uppercase to LowerCase
Write a program to convert Uppercase to LowerCase
Examples:
Input: str= “w3wiki”
Output: “w3wiki”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= "w3wiki" ; 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 = "w3wiki" ; // 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 = "w3wiki" # 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 = "w3wiki" ; // 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 = "w3wiki" ; // 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
w3wiki
Time Complexity: O(N)
Auxiliary space: O(1)
Related Article: