summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cst120/week_5/BUILD3
-rw-r--r--cst120/week_5/quiz_5.c97
2 files changed, 100 insertions, 0 deletions
diff --git a/cst120/week_5/BUILD b/cst120/week_5/BUILD
new file mode 100644
index 0000000..6d2f536
--- /dev/null
+++ b/cst120/week_5/BUILD
@@ -0,0 +1,3 @@
+load("//:rules.bzl", "assignment")
+
+assignment("quiz_5")
diff --git a/cst120/week_5/quiz_5.c b/cst120/week_5/quiz_5.c
new file mode 100644
index 0000000..f2edc9a
--- /dev/null
+++ b/cst120/week_5/quiz_5.c
@@ -0,0 +1,97 @@
+/* Author: Zoltan Szabatin
+ * Professor: Professor Phong N.
+ * Project: Quiz 5
+ * Course: CST 120 */
+
+/* Disable security warnings when using MSVC */
+/* #ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif */
+
+#include <ctype.h> /* toupper */
+#include <linux/limits.h> /* PATH_MAX */
+#include <stdio.h> /* printf, scanf */
+#include <stdlib.h> /* malloc, realloc, size_t */
+#include <string.h> /* strlen */
+
+/* Rail Fence cipher encryption */
+char *rail_fence_encrypt(const char *, size_t);
+
+/* Convert a string to uppercase; destructively */
+void string_to_uppercase(char *);
+
+int main(void) {
+ /* Mutable variables for user input */
+ int user_rails = 0;
+ char user_go_again = 'z';
+ char user_message[PATH_MAX] = {0};
+
+ /* Keep running while the user requests so */
+ do {
+ char *rail_fence_encrypted_message = NULL;
+
+ /* Prompt and accept cipher message */
+ printf("Enter a string: ");
+ scanf(" %s", user_message);
+
+ /* Prompt and accept cipher key */
+ printf("Enter rail: ");
+ scanf(" %d", &user_rails);
+
+ /* Encrypt user's message by the Rail Fence cipher
+ */
+ rail_fence_encrypted_message =
+ rail_fence_encrypt(user_message, (size_t)user_rails);
+
+ string_to_uppercase(rail_fence_encrypted_message);
+
+ /* Output final cipher-ed result */
+ printf("Encrypted text: %s\n", rail_fence_encrypted_message);
+
+ /* Prompt user to repeat */
+ printf("\nWould you like to go again? (y/n) ");
+ scanf(" %c", &user_go_again);
+ printf("\n");
+
+ /* Free the encrypted messages */
+ free((void *)rail_fence_encrypted_message);
+ } while (user_go_again == 'y' || user_go_again == 'Y');
+
+ return 0;
+}
+
+char *rail_fence_encrypt(const char *message, size_t rails) {
+ size_t message_index = 0, encrypted_message_index = 0, rail_index = 0;
+ size_t message_length = (size_t)strlen(message);
+ char *encrypted_message = malloc((message_length + 1) * sizeof(char));
+
+ for (rail_index = 0; rail_index < rails; ++rail_index) {
+ /* Obtain and append message's rail to the encrypted message for the number
+ * of rails */
+ for (message_index = rail_index; message_index < message_length;
+ message_index += rails) {
+ encrypted_message[encrypted_message_index++] = message[message_index];
+ }
+ }
+
+ /* If the message length is not a multiple of the number of rails, append 'x's
+ * to the end. */
+ while (encrypted_message_index < message_length) {
+ encrypted_message = realloc(encrypted_message,
+ (encrypted_message_index + 2) * sizeof(char));
+ encrypted_message[encrypted_message_index++] = 'x';
+ }
+
+ /* Null terminator to signify C-string status */
+ encrypted_message[message_length + 1] = '\0';
+
+ return encrypted_message;
+}
+
+void string_to_uppercase(char *string) {
+ /* Convert each letter to upper-case, one-by-one */
+ while (*string) {
+ *string = (char)toupper(*string);
+ string += 1;
+ }
+}