add lab04 solution
This commit is contained in:
8 files changed
+144
No files matched your search
Binary file not shown.
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
Science and technology are driving forces for innovation.
|
||||
@@ -0,0 +1 @@
|
||||
Elephants are the largest land animals on Earth.
|
||||
@@ -0,0 +1,97 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// 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 <input_file> <output_file> <shift>\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;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Hohskdqwv duh wkh odujhvw odqg dqlpdov rq Hduwk.
|
||||
@@ -0,0 +1 @@
|
||||
Vflhqfh dqg whfkqrorjb duh gulylqj irufhv iru lqqrydwlrq.
|
||||
@@ -0,0 +1 @@
|
||||
Hohskdqwv duh wkh odujhvw odqg dqlpdov rq Hduwk.
|
||||
Reference in new issue
Block a user