1
+ #include <assert.h>
2
+ #include <string.h>
3
+
4
+ /**
5
+ *
6
+ * @param original the origin text.
7
+ * @param steps the steps to be moved.
8
+ * @return the string encrypted.
9
+ */
10
+ char * encrypt (char * original , int steps ) {
11
+ for (int i = 0 ; original [i ] != '\0' ; ++ i ) {
12
+ if (original [i ] >= 'A' && original [i ] <= 'Z' ) {
13
+ original [i ] = (char ) ('A' + (original [i ] - 'A' + steps ) % 26 );
14
+ } else if (original [i ] >= 'a' && original [i ] <= 'z' ) {
15
+ original [i ] = (char ) ('a' + (original [i ] - 'a' + steps ) % 26 );
16
+ }
17
+ }
18
+ return original ;
19
+ }
20
+
21
+ /**
22
+ * Decrypt a string.
23
+ * @param password the password to be decrypted.
24
+ * @param steps the steps to be moved.
25
+ * @return the string decrypted.
26
+ */
27
+ char * decrypt (char * password , int steps ) {
28
+ for (int i = 0 ; password [i ] != '\0' ; ++ i ) {
29
+ if (password [i ] >= 'A' && password [i ] <= 'Z' ) {
30
+ password [i ] = (char ) ('A' + (password [i ] - 'A' - steps + 26 ) % 26 );
31
+ } else if (password [i ] >= 'a' && password [i ] <= 'z' ) {
32
+ password [i ] = (char ) ('a' + (password [i ] - 'a' - steps + 26 ) % 26 );
33
+ }
34
+ }
35
+ return password ;
36
+ }
37
+
38
+ void test () {
39
+ char original [100 ] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
40
+ assert (strcmp ("DEFGHIJKLMNOPQRSTUVWXYZABC" , encrypt (original , 3 )) == 0 );
41
+
42
+ strcpy (original , "abcdefghijklmnopqrstuvwxyz" );
43
+ assert (strcmp ("defghijklmnopqrstuvwxyzabc" , encrypt (original , 3 )) == 0 );
44
+
45
+ char password [100 ] = "DEFGHIJKLMNOPQRSTUVWXYZABC" ;
46
+ assert (strcmp ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" , decrypt (password , 3 )) == 0 );
47
+
48
+ strcpy (password , "defghijklmnopqrstuvwxyzabc" );
49
+ assert (strcmp ("abcdefghijklmnopqrstuvwxyz" , decrypt (password , 3 )) == 0 );
50
+ }
51
+
52
+ int main () {
53
+ test ();
54
+ return 0 ;
55
+ }
0 commit comments