No results found for the specified position. Return the string in lowercase. Java Beginner Level Strings Mock Interview

MockQuestions

Java Beginner Level Strings Mock Interview

Question 2 of 11 for our Java Beginner Level Strings Mock Interview

Get More Information About Our Java Beginner Level Strings Interview Questions

Question 2 of 11

Return the string in lowercase.

This interview question tests the developer's skills with Java strings and loops.

You are given a string. The string contains the English alphabet. You need to implement a function toLowerCase which returns the same string in lowercase letters. You can not use the inbuilt function String.prototype.toLowerCase.

/*Example*/

given string- “abcAGiHkj”
expected output- “abcagihkj”

given string- “qwert”
expected output- “qwert”

Solution:

We will iterate over each character of the given string and convert it into lowercase. Then we will push the resulting character into a result array. After iterating the string, we will join the result array and return it. Now the problem remaining is to convert a character to lowercase.

The lowercase letters have character codes from 97 to 122 (both inclusive) and uppercase have from 65 to 90 (both inclusive). We will check the code of the letter. If it is between 97 and 122, we need not change the character. If it is between 65 and 90, we will add 32 (= 97 - 65) to convert to lowercase. So if-
the letter is “j”
-> its code is 106
-> between 97 and 122
-> already a lowercase letter
the letter is “B”
-> its code is 66
-> between 65 and 90
-> add 31
-> 66 + 32 = 98
-> 98 is “b”

class Solution {
    public String toLowerCase(String str) {
        StringBuilder result = new StringBuilder();
 
        for (char ch : str.toCharArray()) {
            int charCode = ch;
            
            // if it uppercase
            if (charCode >= 65 && charCode <= 90) {
                charCode += 32;
            }
            
            char lowerChar = (char) charCode;
            result.append(lowerChar);
        }
        
        return result.toString();
    }
    
}

Time complexity- O(n)
Space complexity- O(n)

Written by on June 27th, 2021

Next Question

How to Answer: Return the string in lowercase.

Advice and answer examples written specifically for a Java Beginner Level Strings job interview.

  • 2. Return the string in lowercase.

      This interview question tests the developer's skills with Java strings and loops.

      You are given a string. The string contains the English alphabet. You need to implement a function toLowerCase which returns the same string in lowercase letters. You can not use the inbuilt function String.prototype.toLowerCase.

      /*Example*/
      
      given string- “abcAGiHkj”
      expected output- “abcagihkj”
      
      given string- “qwert”
      expected output- “qwert”

      Solution:

      We will iterate over each character of the given string and convert it into lowercase. Then we will push the resulting character into a result array. After iterating the string, we will join the result array and return it. Now the problem remaining is to convert a character to lowercase.

      The lowercase letters have character codes from 97 to 122 (both inclusive) and uppercase have from 65 to 90 (both inclusive). We will check the code of the letter. If it is between 97 and 122, we need not change the character. If it is between 65 and 90, we will add 32 (= 97 - 65) to convert to lowercase. So if-
      the letter is “j”
      -> its code is 106
      -> between 97 and 122
      -> already a lowercase letter
      the letter is “B”
      -> its code is 66
      -> between 65 and 90
      -> add 31
      -> 66 + 32 = 98
      -> 98 is “b”

      class Solution {
          public String toLowerCase(String str) {
              StringBuilder result = new StringBuilder();
       
              for (char ch : str.toCharArray()) {
                  int charCode = ch;
                  
                  // if it uppercase
                  if (charCode >= 65 && charCode <= 90) {
                      charCode += 32;
                  }
                  
                  char lowerChar = (char) charCode;
                  result.append(lowerChar);
              }
              
              return result.toString();
          }
          
      }

      Time complexity- O(n)
      Space complexity- O(n)

      Written by S. Kumar on June 27th, 2021