Here is a program in Java to move all uppercase characters to the end of a given string:
import java.util.Arrays; public class Main { public static void main(String[] args) { String input = "MoveAllUpperCaseToEnd"; char[] characters = input.toCharArray(); int len = characters.length; int i = 0, j = len - 1; while (i < j) { if (Character.isUpperCase(characters[i])) { char temp = characters[i]; characters[i] = characters[j]; characters[j] = temp; j--; } else { i++; } } String output = new String(characters); System.out.println("Input String: " + input); System.out.println("Output String: " + output); } }
Explanation:
The input string is converted into a character array using the toCharArray()
method.
Two pointers i
and j
are initialized. i
points to the start of the array and j
points to the end.
In each iteration, the program checks if the character at the i
th index is uppercase.
If it is, the character is swapped with the character at the j
th index, and j
is decremented.
If it is not uppercase, i
is incremented.
The loop continues until i
is greater than or equal to j
.
The final character array is converted back into a string using the String
constructor.
The input and output strings are printed to the console.