Contents

Emphasize A (with.Java)

   Oct 14, 2023     1 min read

This is an article about emphasizing A.

We’re going to be solving coding test questions, reflecting on the problems we solved, and learning about other ways to solve them.

Let’s start with the problem.

Problem

You are given a string myString.

Complete the solution function by converting all occurrences of the alphabet “a” in myString to “A” and all non-capitalized alphabets to lowercase alphabets and return.

Example input and output
myStringresult
“abstract algebra”“AbstrAct AlgebrA”

My solution to the problem

class Solution {
    public String solution(String myString) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < myString.length(); i++) {
            char currentChar = myString.charAt(i);
            if (currentChar == 'a') {
                result.append('A');
            }else if(currentChar == 'A'){
                result.append(currentChar);
            }else if (Character.isUpperCase(currentChar)) {
                result.append(Character.toLowerCase(currentChar));
            }else {
                result.append(currentChar);
            }
        }
        } return result.toString();
    }
}
Solution Explained

The above code converts the string by traversing the given string character by character and checking for conditions.

If the character is ‘A’, it is kept as is, and if it is in the uppercase alphabet, it is converted to lowercase.

Other characters are kept intact.

Considering the performance of string operations, it is implemented to use StringBuilder to construct the string.