-
Notifications
You must be signed in to change notification settings - Fork 0
/
fused.java
152 lines (133 loc) · 3.53 KB
/
fused.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.Scanner;
public class fused
{
void encode()
{
//calling Scanner class
Scanner sc = new Scanner(System.in);
//prompting user to enter a string
System.out.println("please enter a string");
String s = sc.nextLine();
//declaration and initialisation
String result = "" ;
//loop to CopyOfencode the character
for(int i = 0 ; i < s.length() ; i++ )
{
char ch = s.charAt(i);
int temp = ch + 3 ;
if(ch == '?')
{
temp = 63;
}
else if( ch == ' ')
{
temp = 32 ;
}
else if(Character.isUpperCase(ch))
{
if(temp > 90)
{
temp = temp - 26 ;
}
else if (temp < 65)
{
temp = temp + 26 ;
}
}
else if(Character.isLowerCase(ch))
{
if(temp > 122)
{
temp = temp - 26 ;
}
else if(temp < 97 )
{
temp = temp + 26 ;
}
}
result = result +(char)temp;
}
//declaration
String reverse = "" ;
// reversng the code
for(int i = 0; i < result.length(); i++)
{
char ch = result.charAt(i);
reverse = ch + reverse;
}
//displat statement
System.out.println(reverse);
}
void decode()
{
//calling Scanner class
Scanner sc = new Scanner(System.in);
//prompting user to enter a string
System.out.println("please enter a string");
String s = sc.nextLine();
// declaration and initialisation
String r = "" ;
//declaration
String reverse = "" ;
// reversng the code
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
reverse = ch + reverse;
}
s = reverse;
//loop to encode the character
for(int i = 0 ; i < s.length() ; i++ )
{
char ch = s.charAt(i);
int temp = ch - 3 ;
if(ch == '?')
{
temp = 63;
}
else if(ch == ' ')
{
temp = 32 ;
}
else if(Character.isUpperCase(ch))
{
if(temp > 90)
{
temp = temp - 26 ;
}
else if (temp < 65)
{
temp = temp + 26 ;
}
}
else if(Character.isLowerCase(ch))
{
if(temp > 122)
{
temp = temp - 26 ;
}
else if(temp < 97 )
{
temp = temp + 26 ;
}
}
r = r + (char)temp ;
}
//display statement
System.out.println(r);
}
public static void main(String args[])
{
fused obj = new fused();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("1. Encode\n2. Decode");
int option = sc.nextInt();
if(option == 1)
obj.encode();
else if(option == 2)
obj.decode();
}
}
}