-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.java
70 lines (62 loc) · 1.74 KB
/
encode.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
// This code was contributed by https://github.com/voyager2005
import java.util.Scanner;
public class encode
{
public static void main(String args[])
{
//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 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 ;
}
}
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);
}
}