-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple Text Editor.java
More file actions
83 lines (76 loc) · 2.15 KB
/
Simple Text Editor.java
File metadata and controls
83 lines (76 loc) · 2.15 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb=new StringBuilder();
static int cursor=0;
static void insert(String value)
{
sb.append(value);
}
static void delete(String value)
{
int end=value.length();
int l=sb.length();
sb.delete(l-end,l-1);
}
static char get(int value)
{
String str=sb.toString();
cursor+=value-1;
return str.charAt(cursor);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sentence=sc.nextLine();
StringTokenizer st=new StringTokenizer(sentence,",");
int n=st.countTokens();
int keys[]=new int[n];
String values[]=new String[n];
int i=0;
while(st.hasMoreTokens())
{
String s=st.nextToken();
StringTokenizer temp=new StringTokenizer(s);
keys[i]=Integer.parseInt(temp.nextToken());
if(keys[i]==4)
{
values[i]="";
}
else
{
values[i]=temp.nextToken();
}
i++;
}
for(int j=0;j<n;j++){
if(keys[j]==1)
{
insert(values[j]);
}
else if(keys[j]==2)
{
delete(values[j]);
}
else if(keys[j]==3)
{
System.out.println(get(Integer.parseInt(values[j])));
}
else if(keys[j]==4)
{
int prev=j-1;
if(keys[prev]==1)
{
insert(values[prev]);
}
else if(keys[prev]==2)
{
delete(values[prev]);
}
else if(keys[prev]==3)
{
System.out.println(get(Integer.parseInt(values[prev])));
}
}
}
}
}