-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_editor.java
More file actions
106 lines (101 loc) · 2.85 KB
/
Copy pathtext_editor.java
File metadata and controls
106 lines (101 loc) · 2.85 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Question asked in coding round of Smartprix.
Write a program to simulate a keyboard with given Keys and their operation .Type of keys to simulate were: Alphanumeric space: print it as it is and shift cursor.
@ = toggles caps lock on if off and vice versa, initially it is off.
# = inserts a new line at cursor position and shift cursor.
/ = deletes one character at left and points cursor at that position.
? = works as down arrow if cursor is at last line nothing changes.
^ = works as up arrow if cursor is at first line nothing changes.
*/
import java.util.*;
class D{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String inp;
inp="asdf#q#pqr^^23";//sc.nextLine();
List<StringBuilder> l=new LinkedList<StringBuilder>();
StringBuilder s=new StringBuilder("");
l.add(s);
int i, len=inp.length(), caps=0, x=0, j=0, cc=0;
char c;
for(i=0;i<len;i++)
{//we use cc as index at which insertion
c=inp.charAt(i);
if(Character.isLetterOrDigit(c)||(c==' '))
{
if(caps==1 && Character.isLetter(c))
c=Character.toUpperCase(c);
if(cc<=l.get(x).length())
l.get(x).insert(cc,c);
else
l.get(x).append(c);//OR l.get(x).insert(l.get(x).length(), c);
cc++;
}
else if(c=='<')
{
if(cc==0)
{
if(x>0)
{
x--;//current decremented
cc=l.get(x).length();//if not enough chars in this line current col to the end
}
}
else
cc--;
}
else if(c=='>')
{
if(cc==l.get(x).length())
{
if(x<j)
{
x++;//current row changed
cc=0;//to start of next line
}
}
else cc++;
}
else if(c=='@')
caps=1-caps;
else if(c=='?' && x<j)//!!!!!!!!!IF KEY PRESSED CONTINUOUSLY
{
x++;//current row incremented
if(i==len-1|| (inp.charAt(i+1)!=c && l.get(x).length()<cc))//=>only if c is the last of its kind
cc=l.get(x).length(); //if not enough chars in this line current col to the end
}
else if(c=='^' && x>0)//!!!!!!!!!IF KEY PRESSED CONTINUOUSLY
{
x--;//current row decremented
if(i==len-1 || (inp.charAt(i+1)!=c && l.get(x).length()<cc))//=>only if c is the last of its kind
cc=l.get(x).length(); //if not enough chars in this line current col to the end
}
else if(c=='/')//backspace
{
if(cc>0)
{
l.get(x).deleteCharAt(cc-1);//remove previous character & shift current col left
cc--;
}
else if(cc==0 && x>0)
{
cc=l.get(x-1).length();//current line merged with previous line & current column to the end of original previous line
l.get(x-1).append(l.get(x));
l.remove(x);//remove current line
x--;//current row decremented
j--;//total rows decremented
}
}
else if(c=='#')
{
j++;//add new line
StringBuilder s1=new StringBuilder("");
l.add(s1);
cc=0;x++;//current column 0, current row incremented
}
}
for(i=0;i<l.size();i++)
System.out.println(l.get(i));
}
}