forked from 14visheshjain/DataStructure-Algorithm-in-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreverseinmath.java
More file actions
47 lines (38 loc) · 813 Bytes
/
reverseinmath.java
File metadata and controls
47 lines (38 loc) · 813 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
37
38
39
40
41
42
43
44
45
46
47
/*
* @author Vishesh Jain
* @date 27-Feb-2019
*/
package lecture6;
import java.util.Scanner;
public class reverseinmath {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String s = a.next();
System.out.println(reverse(s));
}
public static String reverse(String s) {
String newst = "";
int end = s.length();
int begin = s.length() - 1;
int j = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (Character.isDigit(s.charAt(i))) {
begin = i;
j = 1;
} else {
if (j == 0) {
newst = newst + s.charAt(i);
end = i;
j = 0;
}
else { newst = newst + s.substring(begin, end);
newst = newst + s.charAt(i);
end = i;
j = 0;}
}
}
if (j == 1)
newst = newst + s.substring(begin, end);
return newst;
}
}