-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReverse.java
More file actions
31 lines (26 loc) · 1.02 KB
/
Reverse.java
File metadata and controls
31 lines (26 loc) · 1.02 KB
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
class Reverse
{
public static void main(String args[])
{
String a = "sample";
int len=a.length();
System.out.println(a);
char[] b= new char[len];
for(int i=0;i<len;i++)
{
b[len-1-i]=a.charAt(i);//only change comes here
}
for(int i=0;i<len;i++)
{
System.out.print(b[i]);
}
}
}
/*
when we store in reverse direction we start from the last element to store the first element of the array.
in qs, it was only given b[len-1] this part is not changing so always the last letter is overwritten every time the loop runs all other elements still empty(except the last element).
to store all elements in reverse order it should start at last and move towards the 0 indexes of the array.
by giving b[len-1-i] for i=0 b[len-1-i] will be b[len-1] //that is the last element .
when i =1 b[len-i-1] will be the second last element when I increment further element being stored is moving towards the 0 indexes as this Reverse works fine
\\hope it is simple happy coding ♥
*/