-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_string.java
More file actions
36 lines (32 loc) · 1000 Bytes
/
Reverse_string.java
File metadata and controls
36 lines (32 loc) · 1000 Bytes
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
import java.util.*;
public class Reverse_string {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String which You want to reverse:");//Ritesh Kumar=hsetiR ramuK
StringBuilder sb = new StringBuilder(sc.nextLine());
int i = 0;
int j = 0;
int n = sb.length();
while (j < n) {
if (sb.charAt(j) != ' ') {
j++;
} else {
reverse(sb, i, j - 1);
i = j + 1;
j = i;
}
}
reverse(sb, i, j - 1);
System.out.println(sb);
sc.close();
}
public static void reverse(StringBuilder sb, int i, int j) {
while (i <= j) {
char temp = sb.charAt(i);
sb.setCharAt(i, sb.charAt(j)); // Corrected line
sb.setCharAt(j, temp);
i++;
j--;
}
}
}