-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathGFG.java
44 lines (38 loc) · 1.14 KB
/
GFG.java
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
37
38
39
40
41
42
43
44
//Reverse an array without affecting special characters
class GFG
{
public static void reverse(char str[])
{
// Initialize left and right pointers
int r = str.length - 1, l = 0;
// Traverse string from both ends until
// 'l' and 'r'
while (l < r)
{
// Ignore special characters
if (!Character.isAlphabetic(str[l]))
l++;
else if(!Character.isAlphabetic(str[r]))
r--;
// Both str[l] and str[r] are not spacial
else
{
char tmp = str[l];
str[l] = str[r];
str[r] = tmp;
l++;
r--;
}
}
}
// Driver Code
public static void main(String[] args)
{
String str = "a!!!b.c.d,e'f,ghi";
char[] charArray = str.toCharArray();
System.out.println("Input string: " + str);
reverse(charArray);
String revStr = new String(charArray);
System.out.println("Output string: " + revStr);
}
}