-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse.java
More file actions
31 lines (23 loc) · 1013 Bytes
/
Reverse.java
File metadata and controls
31 lines (23 loc) · 1013 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
public class Reverse {
// public static void main(String[] args) { // time complexity - 0n2
// String str = "Hello World";
// String rev = "";
// for(int i = str.length()-1 ; i>=0 ; i--){
// rev = rev + str.charAt(i);
// }
// System.out.println("Original String: " + str);
// System.out.println("Reversed String: " + rev);
// }
public static void main(String[] args) {
String str = "Hello hy";
char[] arr = str.toCharArray();
int leng = str.length();
for(int i=0 ; i< leng/2; i++){ // first aur last value ko replace kare , swaping lga ka
char temp = arr[i];
arr[i] = arr[leng - i - 1]; // ch[i] pehla hogya , length - 1 last hogya , aur length - i iss liya lagaye kyuki
arr[leng - i - 1] = temp; //string aage bhi jaugi
}
String reversedd = new String(arr);
System.out.println(reversedd);
}
}