0

Hey guys :) I've been wracking my brain for ages on my code for pset2 (Substitution) and would really appreciate some help.

I've created a function to convert a string into its uppercase variant, which works perfectly when run by itself. I've made an encrypt function that encrypts a line of text, so long as the text and key are both in uppercase. And finally I've made a function that takes two strings as input, and outputs the second string with the format of the first string (i.e. reformat("HeLlO", "VKXXN") outputs "VkxXN".

All these functions work in their individual programmes. But when I put them together in my substitution programme as follows, things start going wrong and I can't work out why (right now, no ciphertext is printed out):

        string plaintext = get_string("plaintext: ");
        string text_uc = make_uppercase(plaintext);
        string key_uc = make_uppercase(argv[1]);
        string cipher_uc = encrypt(text_uc, key_uc);
        string cipher = reformat(plaintext, cipher_uc);
        printf("ciphertext: %s\n", cipher);
        return 0;

Here's all my code in case the issue lies somewhere in the functions themselves (but I don't understand why that would be the case given all the functions work individually):

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

int check_key_alphabetical(string s);
int check_key_repeats(string s);
string make_uppercase(string s);
string encrypt(string s, string k);
string reformat(string x, string y);


int main(int argc, string argv[])
{

    //Check that there are the correct number of command line arguments)

    if (argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    //Check that key has 26 characters

    else if (strlen(argv[1]) != 26)
    {
        printf("Key must contain 26 characters.\n");
        return 1;
    }

    //Check that key only has alphabetic characters

    else if (check_key_alphabetical(argv[1]) != 26)
    {
        printf("Key must only contain alphabetic characters.\n");
        return 1;
    }

    //Check that key does not contain any repeated letters

    else if (check_key_repeats(argv[1]) != 0)
    {
        printf("Key must not contain repeated characters.\n");
        return 1;
    }

    //Ask for text and print encrypted version of text

    else
    {
        string plaintext = get_string("plaintext: ");
        string text_uc = make_uppercase(plaintext);
        string key_uc = make_uppercase(argv[1]);
        string cipher_uc = encrypt(text_uc, key_uc);
        string cipher = reformat(plaintext, cipher_uc);
        printf("ciphertext: %s\n", cipher);
        return 0;
    }
}

//Function to determine number of aphabetic characters in string

int check_key_alphabetical(string s)
{
    int key_alphabet_count = 0;
    for (int i = 0; i < strlen(s); i++)
    {
        if (isalpha(s[i]) != 0)
        {
          key_alphabet_count++;
        }
    }
    return key_alphabet_count;
}

//Function to determine whether any characters repeat in key

int check_key_repeats(string s)
{
    int repeat_count = 0;

    //Convert all characters in s to uppercase

    for (int i = 0; i < strlen(s); i++)
    {
        s[i] = toupper(s[i]);
    }

    for (int j = 0; j < strlen(s); j++)
    {
        for (int k = j + 1; k < strlen(s); k++)
        {
            if (s[j] == s[k])
            {
                repeat_count++;
            }
        }
    }
    return repeat_count;
}

//Function to encrypt a string (s) using a key (k)

string encrypt(string s, string k)
{
    char c[strlen(s) + 1];
    c[strlen(s)] = '\0';
    string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (int i = 0; i < strlen(s); i++)
    {
        for (int j = 0; j < strlen(alphabet); j++)
        {
            if (isalpha(s[i]) != 0)
            {
                if (s[i] == alphabet[j])
                {
                    c[i] = k[j];
                    break;
                }
            }
            else
            {
                c[i] = s[i];
                break;
            }
        }
   }
   string c2 = c;
   return c2;
}

//Function to create an uppercase copy of a string

string make_uppercase(string s)
{
    char suc[strlen(s) + 1];
    suc[strlen(s)] = '\0';
    for (int i = 0; i < strlen(s); i++)
    {
        suc[i] = toupper(s[i]);
    }
    string suc2 = suc;
    return suc2;
}

//Function to format a string with the uppercase/lowercase format of another string

string reformat(string x, string y)
{
    char format[strlen(x) + 1];
    format[strlen(x)] = '\0';
    
    for (int i = 0; i < strlen(x); i++)
    {
        if (islower(x[i]) != 0)
        {
            format[i] = tolower(y[i]);
        }
        else
        {
            format[i] = y[i];
        }
    }
    string format2 = format;
    return format2;
}

You must log in to answer this question.

Browse other questions tagged .