Problem Set 2 (Caesar) Cipher Program

Program to cipher text inputted at the command line along with a cipher key. Multiple filters are used to make sure the user enters the correct information.

/*
 * File Name: caesar.c
 * Programer: Julio Ornelas
 * Date: 2022/12/28
 * Description of program
 */

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>

//function prototypes
bool only_digits(string isNumber);
string textCipher(string textToCipher, int keyToCipherText);

int main(int argc, string argv[]) //command line arguments argc = agument count and argv = argument vector (array of strings)
{
    //declarations
    string userText; //Variable that will store text inputed by user at prompt
    int cipherKey; //Variable that will store the cipher key inputed at the command line


    //get user input, calculation and output
    if (argc != 2) //if argument count is not equal to 2 quit program and return to command line, instructions are given
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    else
    {
        //continue with program
        if (only_digits(argv[1]) == false)
        {
            printf("Usage: ./caesar key\n"); //if user does not input only numbers as second argument quit program and return to command line, instructions are given again
            return 1;
        }
        else
        {
            //continue with program
            cipherKey = atoi(argv[1]);//cast string as integer
            userText = get_string("plaintext: "); //request text input from user
            textCipher(userText, cipherKey);//function to cipher text and output result
        }
    }
}//int main(int argc, string argv[])

//Function takes in a string and returns true if the string is a number and false if it not a number
bool only_digits(string isNumber)
{
    int userKeyCount;
    userKeyCount = strlen(isNumber);

    for (int i = 0; i < userKeyCount; i++)
    {
        if (isNumber[i] < 48 || isNumber[i] > 57)
        {
            return false;
        }
    }
    return true;
}//bool only_digits(string isNumber)

//This function takes two parameters a string to be cipher and a integer to be used as a key to cipher the text
string textCipher(string textToCipher, int keyToCipherText)
{
    //declarations
    string userText; //Variable that will store the text to be cipher
    int cipherKey; //Varialbe the will hold the Key to be use to cipher the user text
    int value; //ASCII value of each character looped from userText
    int characterLength; //How long a string is

    const int GET_CHAR_VALUE_TO_ZERO_LOWER = 97; //distance from 0 to a specific character (Lower case letter) in a ASCII table
    const int GET_CHAR_VALUE_TO_ZERO_UPPER = 65; //distance from 0 to a specific character (Upper case letter) in a ASCII table

    //user input
    userText = textToCipher;
    cipherKey = keyToCipherText;
    characterLength = strlen(userText);
    cipherKey = cipherKey % 26;

    //calculation and output
    printf("ciphertext: ");
    for (int i = 0; i < characterLength; i++)
    {

        if (userText[i] >= 65 && userText[i] <= 90) //boundaries of uppercase caracters in a ASCII table
        {
            value = userText[i] - 65;
            value = (value + cipherKey) % 26;
            value = value + 65;
            printf("%c", value);
        }
        else if (userText[i] >= 97 && userText[i] <= 122) //boundaries of lowercase caracters in a ASCII table
        {
            value = userText[i] - 97;
            value = (value + cipherKey) % 26;
            value = value + 97;
            printf("%c", value);
        }
        else
        {
            //every other character in the ASCII table
            printf("%c", userText[i]);
        }
    }//for (int i = 0; i < characterLength; i++)
    printf("\n");
    return 0;
}//string textCipher(string textToCipher, int keyToCipherText)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *