Skip to content

Commit 4040091

Browse files
jbhattacjbhattac
authored andcommitted
ReverseArrayWithoutAffectingSpecialChars
1 parent ec978cd commit 4040091

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ds.algo.solutions;
2+
3+
public class ReverseArrayWithoutAffectingSpecialChars {
4+
5+
public static void main(String[] args) {
6+
String str = "Ab,c,de!$";
7+
char[] arr = str.toCharArray();
8+
9+
int start = 0;
10+
int end = arr.length - 1;
11+
char temp;
12+
13+
System.out.println("Before");
14+
System.out.println(arr);
15+
16+
while (start < end) {
17+
if (isAlphabet(arr[start]) && isAlphabet(arr[end])) {
18+
temp = arr[start];
19+
arr[start] = arr[end];
20+
arr[end] = temp;
21+
start++; end--;
22+
continue;
23+
}
24+
if (!isAlphabet(arr[start])) {
25+
start++;
26+
}
27+
if (!isAlphabet(arr[end])) {
28+
end--;
29+
}
30+
}
31+
32+
33+
System.out.println("After");
34+
System.out.println(arr);
35+
}
36+
37+
private static boolean isAlphabet(char ch) {
38+
return ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122;
39+
}
40+
}

0 commit comments

Comments
 (0)