-
Notifications
You must be signed in to change notification settings - Fork 0
/
InnermostParrenthesis.java
49 lines (46 loc) · 1.01 KB
/
InnermostParrenthesis.java
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
48
49
package csd;
public class InnermostParrenthesis {
public static void main(String[] args) {
TestParenthesis.run();
}
}
class TestParenthesis {
public static void run()
{
UtilParenthesis.getSentenceIn("Bur örnek { aslýnda {hi{ç zo}r}} deðil.");
}
}
class UtilParenthesis {
public static int leftParenthesis(String s)
{
int i, indexBegin = 0;
int len = s.length();
for(i = 0; i < len && s.charAt(i) != '}'; ++i)
{
if(s.charAt(i) == '{')
indexBegin = i;
;
}
return indexBegin;
}
public static int rightParenthesis(String s)
{
int i, indexLast = 0;
int len = s.length();
//döngü gereksiz dönmesin diye ikinci koþul olarak != '{' bunu ekledim.
//Parantezlerin index numaralarýný tuttum.
for(i = len - 1 ; i >= 0 && s.charAt(i) != '{'; --i)
{
if(s.charAt(i) == '}')
indexLast = i;
;
}
return indexLast;
}
public static void getSentenceIn(String s)
{
int begin = leftParenthesis(s);
int last = rightParenthesis(s);
System.out.println(s.substring(begin + 1, last));
}
}