Move all special char to the end of the String in Java29 Mar 2025 | 6 min read To add all special characters to the end of a string in Java, it's necessary to iterate through the input string, identify the alphanumeric letters, and then rearrange them so that the special characters are at the end. Java's built-in character classification methods can be used to distinguish between letters, numerals, and non-alphanumeric characters, which is often necessary for this operation. The outcome should relocate the special characters without changing their relative order while retaining the order of the alphanumeric letters. Example 1: Input: String str = "Hello!@#World123$%" Output: The resulted string is given by HelloWorld123!@#$% Explanation: Special characters (!@#$%) are relocated to the end, maintaining their original order, while all alphanumeric characters (HelloWorld123) are pushed to the front. Example 2: Input: String str = "2024Year!!!New#Goals" Output: The resulted string is given by 2024YearNewGoals!!!# Explanation: In the same arrangement, the special characters (!!!#) are moved to the end, and the alphanumeric characters (2024YearNewGoals) are positioned up front. Example 3: Input: String str = "Java$Is#Fun*&" Output: The resulted string is given by JavaIsFun$#*& Explanation: After the special characters ($#*&), which remain in the same order as before, are the alphanumeric characters (JavaIsFun). Naïve ApproachThe goal is to iterate through the input string and preserve two strings: one with regular characters (a, A, 1, '', etc.) and another with special characters (@, $, etc.). Concatenate the two strings at the end and return. Algorithm:Step 1: Initialize the string s. Step 2: Set up two blank result strings, result1 for alphanumeric characters and result2 for special characters. Step 3: Go through every character in the string s in a loop. Step 4: Verify that ch corresponds to the regular expression [a-zA-Z0-9\\s+]. Step 4.1: Add ch to result1 if that is the case. Step 4.2: Add ch to result2 if it's false. Step 5: Return the combination of result 1 (alphanumeric characters) and result 2 (special characters). Step 6: Print the updated result after invoking the function with the input string. Implementation:FileName: CharactersTraversing.java Output: The resulted string is given by 2024YearNewGoals!!!# Complexity Analysis: The time complexity of the above code is O(N), and the space complexity is O(N), where 'N' represents the length of the string. Approach: Using PointersThe approach provides a Java program that, given a string, splits special characters from alphanumeric letters and spaces, adding the special characters at the end. The moveCharToEnd method checks if each character in the input string str is a space or an alphanumeric character by iterating through each character. If it is, it's added to the red string. If not, it is appended to the Char string, which is used to store special characters. The method concatenates the res and Char strings after the loop and returns the outcome. Algorithm: Step 1: Create two blank strings, called char for special characters and res for alphanumeric characters and spaces. Step 2: Every character in the input string str is iterated through. Step 3: Determine whether each character at index I is a space or an alphanumeric character (inside A, A, 0-9). Step 4: Add the character to res if it's a space or an alphanumeric character. Step 5: If not, append the character (signifying that it is a unique character) to char. Step 6: Concatenate res, which contains alphanumeric characters and spaces, with char, which includes special characters, once the loop is finished. Step 7: Return the concatenated string. It starts with all alphanumeric characters and ends with special characters. Step 8: Print the result that was returned. The sequence of spaces and alphanumeric letters in the original string will be preserved when the special characters are added at the end. Implementation:FileName: PointersCharacterTravaersing.java Output: The resulted string is given by 2024YearNewGoals!!!# Complexity Analysis: The Time Complexity of the above code is O(N), where N represents the length of the input string and the Space Complexity is O(N). Approach: Using 'isalnum()' methodThe below approach provided defines a class called isalnumMethod, which has a moveAllSpecialChars method that divides special characters in an input string into different categories from regular characters (letters, numbers, and spaces). Using character.isLetterOrDigit() and Character.isWhitespace() to determine which characters are regular, the procedure loops through each character in the string. The normalCh string receives these regular characters as an appendix, whereas the specialCh string stores the special characters. Ultimately, all special characters are moved to the end of the two strings by concatenating them. Algorithm:Step 1: Start with an empty string called "special_ch," in order to store the special characters, and to store the regular characters, start with an empty string called "normal_ch." Step 2: Go through every "ch" character in the input string one at a time. Step 3: The steps given below should be followed for each character in the provided string. Step 3.1: Use the isalnum() function to determine whether ch is an alphanumeric character, a space, or a whitespace character. Alternatively, use the isspace() method. Step 3.2: Add ch to the normal_chars string if it's a space or an alphanumeric character. Step 3.3: Add ch to the special_chars string if it's a unique character. Step 4: Give back the concatenation of normal_ch and special_ch after going through every character in the string. Implementation:FileName: IsalnumMethod.java Output: The resulted string is given by 2024YearNewGoals!!!# Complexity Analysis: The Time Complexity of the above code is O(N), where n represents the length of the input string and the Space Complexity is O(N). Next TopicConvert Java object to JSON |
Use of Constructor in Java | Purpose of Constructor in Java
When an object is instantiated in Java, a particular kind of method called a constructor is called. Initialising an object's state is the aim of a constructor. Setting the object's initial settings for its properties and carrying out any other setup necessary to ensure proper operation...
4 min read
Java DatagramSocket and DatagramPacket
classes are used for connection-less socket programming using the UDP instead of TCP. Datagram Datagrams are collection of information sent from one device to another device via the established network. When the datagram is sent to the targeted device, there is no assurance that it will...
4 min read
Water Jug Problem in Java
Water Jug Problem is one of the most important problems to solve in Java. The water jug problem is a problem where we have two jugs, "i" liter jug and "j" liter jug (0 < i < j). Both jugs will initially be empty, and they...
6 min read
Return Statement in Java
What is a return statement in Java? In Java programming, the return statement is used for returning a value when the execution of the block is completed. The return statement inside a loop will cause the loop to break and further statements will be ignored by the...
7 min read
arr.length vs arr[0].length vs arr[1].length in Java
Difference Between arr.length, arr[0].length and arr[1].length in Java Java provides an attribute length that determines the length of an array. Every array has an in-built length property whose value is the size of the array. Size implies the total number of elements that an array can contain....
2 min read
Essentials of Java Programming Language
Java is a versatile and widely used programming language, and has been an integral part of software development for more than two decades. Known for its platform independence, security features and extensive libraries, Java is a language that every aspiring and experienced developer should understand. In...
4 min read
Maximum sum of a Subarray with prime integers in Java
Find the greatest sum of a continuous subarray such that the subarray only consists of prime numbers given an array arr[] of integers of size n. Stated a certain way, no non-prime integer is allowed to exist in the selected subarray. Example 1: Input: int a[] = {...
7 min read
How to add 6 months to Current Date in Java
? In Java, we can add 6 months to the current date using the Calendar or LocalDate class. In this section, we'll discuss both approaches and show how to implement Date classes in Java code. Using the Calendar Class The Calendar class is a legacy class that was introduced...
4 min read
DoubleBuffer equals() method in Java with Examples
There is equality between two double buffers if, and only if the elements are of the same type, there are an equal number of remaining elements, and the two sequences of elements are point-by-point equivalent when taken to be considered irrespective of where they started. The...
4 min read
Difference Between String, StringBuffer, and StringBuilder in Java
A string is a representation of a series of characters. Among the classes that developers most frequently utilise when programming in Java are strings. However, Java created the StringBuilder and StringBuffer utility classes to make manipulating strings easier because they are immutable. String A string is a...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India