-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveSpeacialCharToLast.java
More file actions
36 lines (29 loc) · 1.16 KB
/
MoveSpeacialCharToLast.java
File metadata and controls
36 lines (29 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class MoveSpeacialCharToLast {
// Method to move special characters to the end of the string
public static String moveSpecialCharacters(String str) {
StringBuilder lettersAndDigits = new StringBuilder();
StringBuilder specialChars = new StringBuilder();
// Iterate through each character in the string
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Check if character is letter or digit
if (Character.isLetterOrDigit(ch)) {
lettersAndDigits.append(ch);
} else {
specialChars.append(ch);
}
}
// Combine normal and special characters
return lettersAndDigits.toString() + specialChars.toString();
}
// Main method to test the functionality
public static void main(String[] args) {
// Test input
String input = "He@llo!12#";
// Calling method and storing result
String result = moveSpecialCharacters(input);
// Output the result
System.out.println("Original String: " + input);
System.out.println("Modified String: " + result);
}
}