-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaesarWithModulus.dart
106 lines (99 loc) · 2.79 KB
/
CaesarWithModulus.dart
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
import 'dart:io';
void main(List<String> args) {
print("---------------------- 1 . Enskripsi ---------------------- ");
print("---------------------- 2 . Deskripsi ---------------------- ");
print("---------------------- 3 . Keluar ---------------------- ");
print(Menu());
}
//
dynamic Menu() {
stdout.write("Mau coba ? ");
String pilihan = stdin.readLineSync()!;
// Yes
while (pilihan == "y" || pilihan == "Y" || pilihan == "ya") {
stdout.write("Program apa nihh ? ");
String chooseProgram = stdin.readLineSync()!;
if (chooseProgram == "1") {
return CaesarEnkripsi();
} else if (chooseProgram == "2") {
return CaesarDeskripsi();
} else if (chooseProgram == "3") {
return "Terima Kasih";
} else {
return "Anda Ngantuk ?";
}
}
// No
while (pilihan == "t" || pilihan == "T" || pilihan == "Tidak") {
return "OKEE TERIMA KASIH";
}
}
//
String CaesarEnkripsi() {
print("Fitur Enkripsi");
stdout.write("Masukkan String : ");
String str = stdin.readLineSync()!;
stdout.write("Masukkan Key : ");
int number = int.parse(stdin.readLineSync()!);
// Enskripsi
number = number % 26;
var lowerCaseString = str.toLowerCase();
var abjad = 'abcdefghijklmnopqrstuvwxyz'.split("");
var newString = '';
//
for (var i = 0; i < lowerCaseString.length; i++) {
var CurrentLetter = lowerCaseString[i];
if (CurrentLetter == " ") {
newString += CurrentLetter;
continue;
}
var currentIndex = abjad.indexOf(CurrentLetter);
//
var newIndex = currentIndex + number;
//
if (newIndex > 25) newIndex = newIndex - 26;
//
if (newIndex < 0) newIndex = 26 + newIndex;
//
if (str[i] == str[i].toUpperCase()) {
newString += abjad[newIndex].toUpperCase();
} else {
newString += abjad[newIndex];
}
}
return "Hasil : ${newString}";
}
String CaesarDeskripsi() {
print("Fitur Deskripsi");
stdout.write("Masukkan String : ");
String str = stdin.readLineSync()!;
stdout.write("Masukkan Key : ");
int number = int.parse(stdin.readLineSync()!);
// Deskripsi
number = number % 26;
var lowerCaseString = str.toLowerCase();
var abjad = 'abcdefghijklmnopqrstuvwxyz'.split("");
var newString = '';
//
for (var i = 0; i < lowerCaseString.length; i++) {
var CurrentLetter = lowerCaseString[i];
if (CurrentLetter == " ") {
newString += CurrentLetter;
continue;
}
var currentIndex = abjad.indexOf(CurrentLetter);
//
var newIndex = currentIndex - number;
//
if (newIndex > 25) newIndex = newIndex - 26;
//
if (newIndex < 0) newIndex = 26 + newIndex;
//
if (str[i] == str[i].toUpperCase()) {
newString += abjad[newIndex].toUpperCase();
} else {
newString += abjad[newIndex];
}
}
return "Hasil : ${newString}";
}