diff --git a/lab04/Lab 4.pdf b/lab04/Lab 4.pdf new file mode 100644 index 0000000..5820525 Binary files /dev/null and b/lab04/Lab 4.pdf differ diff --git a/lab04/Makefile b/lab04/Makefile new file mode 100644 index 0000000..e16bacc --- /dev/null +++ b/lab04/Makefile @@ -0,0 +1,42 @@ +# Compiler and flags +CC = gcc +CFLAGS = -Wall -std=c11 + +#Input file +INPUT = input2.txt +OUTPUT = output.txt +REF = ref2.txt +# Source file +SRCS = lab4.c +# Output +TARGET = lab4.out + +# Default target +all: $(TARGET) + +# Rule to build the executable +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) $(SRCS) -o $(TARGET) + + +convert_input: $(INPUT) + dos2unix $(INPUT) + +convert_output: $(OUTPUT) + dos2unix $(OUTPUT) + +check: $(OUTPUT) $(REF) + @diff -q $(OUTPUT) $(REF) > /dev/null; \ + if [ $$? -eq 0 ]; then \ + echo "Pass"; \ + else \ + echo "Fail"; \ + fi + +# Rule to run the program +run: $(TARGET) + ./$(TARGET) $(INPUT) $(OUTPUT) 3 + +# Rule to clean generated files +clean: + rm -f $(TARGET) diff --git a/lab04/input.txt b/lab04/input.txt new file mode 100644 index 0000000..9e07b26 --- /dev/null +++ b/lab04/input.txt @@ -0,0 +1 @@ +Science and technology are driving forces for innovation. \ No newline at end of file diff --git a/lab04/input2.txt b/lab04/input2.txt new file mode 100644 index 0000000..f876720 --- /dev/null +++ b/lab04/input2.txt @@ -0,0 +1 @@ +Elephants are the largest land animals on Earth. \ No newline at end of file diff --git a/lab04/lab4.c b/lab04/lab4.c new file mode 100644 index 0000000..32e7aa8 --- /dev/null +++ b/lab04/lab4.c @@ -0,0 +1,97 @@ +#include +#include + +// A01385066 + +/** + * encrypt_text takes in an input path, encrypts the contents of + * the file from input path by a given shift amount. Then writes + * the encrypted content to the specified output path. + * + * @param inputPath char[] - input path of file to encrypt + * @param outputPath char[] - output path of encrypted file + * @param shift int - number to shift characters by + * @author Braeden Sowinski + */ +void encrypt_text(char inputPath[], char outputPath[], int shift) +{ + + FILE *readFile; + FILE *writeFile; + char character; + + readFile = fopen(inputPath, "r"); + writeFile = fopen(outputPath, "w"); + + if (readFile == NULL) + { + printf("Error opening file: \"%s\"\n", inputPath); + return; + } + + if (writeFile == NULL) + { + printf("Error opening file: \"%s\"\n", outputPath); + return; + } + + // Iterate over characters till end of file + while((character = fgetc(readFile)) != EOF) + { + + if ((character >= 'A' && character <= 'Z') || + (character >= 'a' && character <= 'z')) + { + + /* + * if capital letter and adding shift to character goes beyond 'Z' + * then shift character to 'A' - 1 before adding shift. + */ + if (character <= 'Z' && character >= 'A' && (character + shift) > 'Z') + { + character -= ('Z' + 1) - 'A'; + } + + character += shift; + + /* + * if character goes beyond alphabet (> 'z') then + * shift character back by the number of lower case + * letters. + */ + if (character > 'z') + { + character -= ('z' + 1) - 'a'; + } + } + + fputc(character, writeFile); + } + + fclose(readFile); + fclose(writeFile); + + return; +} + +int main(int argc, char *argv[]) +{ + + if (argc != 4) + { + printf("Usage: %s \n", argv[0]); + return 1; + } + + char *inputPath; + char *outputPath; + int shift; + + inputPath = argv[1]; + outputPath = argv[2]; + shift = atoi(argv[3]); + + encrypt_text(inputPath, outputPath, shift); + + return 0; +} diff --git a/lab04/output.txt b/lab04/output.txt new file mode 100644 index 0000000..29a23a3 --- /dev/null +++ b/lab04/output.txt @@ -0,0 +1 @@ +Hohskdqwv duh wkh odujhvw odqg dqlpdov rq Hduwk. \ No newline at end of file diff --git a/lab04/ref.txt b/lab04/ref.txt new file mode 100644 index 0000000..8b2a207 --- /dev/null +++ b/lab04/ref.txt @@ -0,0 +1 @@ +Vflhqfh dqg whfkqrorjb duh gulylqj irufhv iru lqqrydwlrq. \ No newline at end of file diff --git a/lab04/ref2.txt b/lab04/ref2.txt new file mode 100644 index 0000000..29a23a3 --- /dev/null +++ b/lab04/ref2.txt @@ -0,0 +1 @@ +Hohskdqwv duh wkh odujhvw odqg dqlpdov rq Hduwk. \ No newline at end of file