8000 Merge pull request #20 from Naivedh/code · RockLee444/Java-Algorithms@c7ee57c · GitHub
[go: up one dir, main page]

Skip to content

Commit c7ee57c

Browse files
authored
Merge pull request darpanjbora#20 from Naivedh/code
string reverse without changing special charchters
2 parents b41b067 + 936ac1f commit c7ee57c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

String Reversal/GFG.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Reverse an array without affecting special characters
2+
class GFG
3+
{
4+
public static void reverse(char str[])
5+
{
6+
// Initialize left and right pointers
7+
int r = str.length - 1, l = 0;
8+
9+
// Traverse string from both ends until
10+
// 'l' and 'r'
11+
while (l < r)
12+
{
13+
// Ignore special characters
14+
if (!Character.isAlphabetic(str[l]))
15+
l++;
16+
else if(!Character.isAlphabetic(str[r]))
17+
r--;
18+
19+
// Both str[l] and str[r] are not spacial
20+
else
21+
{
22+
char tmp = str[l];
23+
str[l] = str[r];
24+
str[r] = tmp;
25+
l++;
26+
r--;
27+
}
28+
}
29+
}
30+
31+
// Driver Code
32+
public static void main(String[] args)
33+
{
34+
String str = "a!!!b.c.d,e'f,ghi";
35+
char[] charArray = str.toCharArray();
36+
37+
System.out.println("Input string: " + str);
38+
reverse(charArray);
39+
String revStr = new String(charArray);
40+
41+
System.out.println("Output string: " + revStr);
42+
}
43+
}
44+

0 commit comments

Comments
 (0)
0