Skip to content

Commit 25dd3f5

Browse files
add ciphers (#6)
1 parent 3ab2703 commit 25dd3f5

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(Examples C)
44

55
set(CMAKE_C_STANDARD 99)
66

7+
add_subdirectory(ciphers)
78
add_subdirectory(conversions)
89
add_subdirectory(dynamicprogramming)
910
add_subdirectory(maths)

ciphers/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".c" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
install(TARGETS ${testname} DESTINATION "bin/ciphers")
12+
13+
endforeach( testsourcefile ${APP_SOURCES} )

ciphers/caesar.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)