Convert To Lower Case

Easy
0/40
profile
Contributed by
15 upvotes
Asked in companies
AmazonLivekeeping (An IndiaMART Company)Google inc

Problem statement

You are given a string ‘str’, your task is to convert all the characters of the string into lowercase letters.

For example:
You are given, ‘str’ = ‘AbcdEfgh’, in this string if you convert all the characters into lowercase, the string will be ‘abcdefgg’. Hence it is the answer.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a single integer ‘T’, representing the number of test cases.

The first line of each test case contains a string ‘str’ representing the given string.
Output Format:
For each test case, print a single string representing the given string with all characters in lower case.

Print a separate line for each test case.
Constraints:
1 <= T <=  10
1 <= |str| <= 10^4

‘str’ will contain upper and lower case characters of the English alphabet.
 Time Limit: 1 sec.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function. 
Sample Input 1:
2
AbcdEfgh
AAAAb
Sample Output 1:
abcdefgh
aaaab
Explanation for sample Input 1:
For the first test case, ‘str’ = ‘AbcdEfgh’, in this string if you convert all the characters into lowercase, the string will be ‘abcdefgg’. Hence it is the answer.

For the second test case, ‘str’ = ‘AAAAb’,  in this string if you convert all the characters into lowercase, the string will be ‘aaaab’. Hence it is the answer.
Sample Input 2:
2
abcde
ABCDE
Sample Output 2:
 abcde
 abcde
Hint

Try to process each character one at a time.

Approaches (1)
Brute-Force Method

 

In this approach, we will iterate over the entire string and if we find any character that is in upper case, we can convert it into the lowercase version of itself.

Note: below, ASC(ch) means ASCII value of the character ‘ch’.

 

Algorithm:

  • Initialise an empty string ‘resultString’
  • Iterate ch through ‘str
    • If ch is an upper case character
      • Add ASC(ch) + ASC(‘a’) - ASC(‘A’) converted to a character to ‘resultString’
    • Otherwise, add ch to ‘resultString’
  • Return the ‘resultString’
Time Complexity

O(N), Where N is the length of the string

 

We are iterating over each character of the string once, which will cost O(N) time. Hence the final time complexity of the algorithm is O(N)

Space Complexity

O(1), 

 

We are not using any extra space in this algorithm.

Code Solution
(100% EXP penalty)
Convert To Lower Case
Full screen
Console