File tree Expand file tree Collapse file tree
Reverse an array without affecting special characters Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments