From the course: Learning Java 11

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Move capital letters

Solution: Move capital letters - Java Tutorial

From the course: Learning Java 11

Solution: Move capital letters

- [Instructor] Let's implement this function. To keep track of our new string, we'll use a string builder. We'll also create an int to keep track of where we should insert the next capital letter. We want to move the capital letters to the beginning of the list so it'll start off at 0. Then we'll iterate through the input string. We'll retrieve each character using the charAt method. Now we need to decide at what index should we put this character in our result. This will depend on whether the character is a capital letter. We can use the isUpperCase method from the character class to check if a given character is uppercase. Now, if the character is not uppercase, we can append it to the back of the string builder. Its ordering should remain the same. If the character is uppercase, we'll want to insert it at the beginning of the list. Now, if there's more than one capital letter in the list, this won't work. Each…

Contents