String Reverse Without Reversing the Special Character Positions in Java
Reverse the string such that special characters really aren't impacted, given a string that includes alphabets (from "a" to "z" and "A" to "Z") and special characters.
Example:
Input: str = “c,b$a”
Output: str = “a,b$c”
Explanation: Keep in mind that the values $ and, remain static. Reversing only the subsequence "cba".
Input: str = “Ab,c,de!$”
Output: str = “ed,c,bA!$”
Simple way for solution
- Make a character array with the name temp[].
- Copy alphabetical characters to temp[ from the provided array.
- Apply the common string reversal method to temp[].
- Now iterate through the input string and the temp inside a single loop. Replace any alphabetic characters in the input string with the current temp[] character.
Here this implementation is done with the string reversal algorithm in iterative way as shown:
Input : arr[] = {4, 5, 6}
Output : arr[] = {6, 5, 4}
Input : arr[] = {2, 1, 6, 5}
Output : arr[] = {5, 6, 1, 2}
- Set the beginning and ending indexes to start = 0 and n-1, respectively.
- Swap arr[start] and arr[end] in a loop, and alter the beginning and ending as follows:
- begin = begin +1, and end = end -1
The application of the above strategy is seen below:
File name: Tomper.java
// To demonstrate how and when to flip a string without
// affecting special characters, the following Java code is provided.
import java.util.*;
class Tomper {
public static void rev(char h[], int k, int q)
{
for (int s = l; s < q / 2; s++) {
char tempo = h[s];
h[s] = h[q - 1 - s];
h[q - 1 - s] = tempo;
}
}
public static void reverse(char h[])
{
// making a character array whose size
// is equal to the string's length
char[] tempo = new char[h.len];
int p = 0;
for (int s = 0; s < h.len; s++) {
if (h[s] >= 'p' && h[s] <= 'r'
|| h[s] >= 'P' && h[s] <= 'R') {
// storing elements in array
tempo[p] = h[s];
p++;
}
}
// the character array being reversed
revr(tempo, 0, p);
p = 0;
for (int s = 0; s < h.len; s++) {
if (h[s] >= 'p' && h[s] <= 'r'
|| h[s] >= 'P' && h[s] <= 'R') {
// origional string is being updated
h[s] = tempo[p];
p++;
}
}
String revrStr = new String(h);
System.out.println("The reversed string is: " + revrStr);
}
// Loading the Driver Code
public static void main(String[] args)
{
String str = "Ba,c,de!$";
char[] charArray = str.toCharArray();
reverse(charArray);
}
}
Output:
reversed string is: ed,c,aB!$
Time complexity of program: O(N) where as N being the string's length.
Auxiliary Space of the program: O(N) where as N being the string's length.
Effective Approach:
The above approach requires additional storage and performs two input string traversals, but it has an O(n) time complexity.
Without requiring additional space, we simply reverse with a single traversal. Listed following is the algorithm.
- Assume that the input string is "str[]," with n being its length.
- l = 0, r = n-1
- Because l is smaller than r, perform the actions listed below:
- Perform l++ if str[l] isn't an alphabetic character.
- If str[r] isn't an alphabetic character, then do the following: r—
- If not, then swap str[l] with str[r].
Here are examples of how the above-mentioned algorithm is implemented.
Tomper1.java
// Java example code for reversing an array without
// impacting special characters
import java.io.*;
class Tomper1 {
public static void reverse(char str[])
{
// Set the left and right points to zero.
int q = str.len - 1, k = 0;
// till "k" and "q," traverse the string starting from both ends.
while (k < q) {
// Ignoring the special characters
if (!Character.isanAlphabetic(str[k]))
k++;
else if (!Character.isanAlphabetic(str[q]))
q--;
// Both the strings str[k] and str[q] are not spacial
else {
char tempo = str[k];
str[k] = str[q];
str[q] = tempo;
k++;
q--;
}
}
}
// Loading the Driver Code
public static void main(String[] args)
{
String str = "b!!!a.c.d,e'f,ghi";
char[] charArray = str.toCharArray();
System.out.println("The Input string: " + str);
reverse(charArray);
String revrStr = new String(charArray);
System.out.println("The Output string: " + revrStr);
}
}
Output:
The Input string: b!!!a.c.d,e'f,ghi
The Output string: i!!!h.g.f,e'd,cab