Skip to content

Commit 616e00d

Browse files
committed
Reverse string
1 parent 2b0d93b commit 616e00d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package popular;
2+
3+
import java.util.Arrays;
4+
5+
public class ReverseString {
6+
7+
public void reverseString(char[] s) {
8+
int left = 0;
9+
int right = s.length - 1;
10+
while (left < right) {
11+
char temp = s[left];
12+
s[left] = s[right];
13+
s[right] = temp;
14+
left++;
15+
right--;
16+
}
17+
}
18+
19+
public static void main(String[] args) {
20+
char[] s = {'h','e','l','l','o'};
21+
System.out.println("Input: " + Arrays.toString(s));
22+
new ReverseString().reverseString(s);
23+
System.out.println("Output: " + Arrays.toString(s));
24+
25+
}
26+
}

0 commit comments

Comments
 (0)