简体   繁体   中英

Why do 2 letters not equal to each other show up as equivalent in C?

I am making a program that does a simple encryption to a plaintext and outputs an encrypted ciphertext. Here is an example with YTNSHKVEFXRBAUQZCLWDMIPGJO being the key.

$ ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
plaintext:  HELLO
ciphertext: EHBBQ

You can read the directions here if you would like to know more.

Here is my code:

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

string ciphertext_generator(string key,string plaintext);
bool alphabetic_char_checker(string word);
bool alphabetic_char_repeat_checker(string word);


string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Please enter a key\n ");
        return 1;
    }
    if (strlen(argv[1]) != 26)
    {
        printf("The key must contain 26 characters.\n ");
        return 1;
    }
    if (alphabetic_char_checker(argv[1]) == false)
    {
        printf("The key must only contain alphabetic characters.\n ");
        return 1;
    }
    else if (alphabetic_char_repeat_checker(argv[1]))
    {
        printf("The key must not contain repeated characters.");
        return 1;
    }


    string plaintext = get_string("plaintext: ");
    string ciphertext = ciphertext_generator(argv[1],plaintext);
    printf("ciphertext: %s",ciphertext);
    return 0;
}
string ciphertext_generator(string key,string plaintext)
{
    string ciphertext = plaintext;
    for (int i = 0, k = strlen(ciphertext); i < k; i++)
    {
        bool capital = isupper(ciphertext[i]);
        ciphertext[i] = toupper(ciphertext[i]);
        for (int i2 = 0, a = strlen(alphabet); i2 < a; i2++)
        {
            if (plaintext[i] == alphabet[i2])
            {
                if (capital == true)
                {
                    ciphertext[i] = key[i2];
                }
                else
                {
                    ciphertext[i] = tolower(key[i2]);
                }
            }
        }
    }
    return ciphertext;
}

bool alphabetic_char_checker(string word)
{
    for (int i = 0, w = strlen(word); i < w; i++)
    {
        word[i] = toupper(word[i]);
        if (word[i] < 'A' || word[i] > 'Z')
        {
            return false;
        }
    }
    return true;
}
bool alphabetic_char_repeat_checker(string word)
{
    for (int i = 0, w = strlen(word); i < w; i++)
    {
        word[i] = toupper(word[i]);
        for (int i2 = 0; i2 < w; i2++)
        {
            if (word[i] == word[i2])
            {
                return false;
            }
        }
    }
    return true;
}

My question lies in the ciphertext_generator function that takes in a key and a plaintext and outputs an encrypted ciphertext. What happens here is that it iterates over ever letter in the plaintext and then for each letter, it iterates over the entire alphabet checking to see if the letter in the plaintext is in the alphabet and counts which index it is in the alphabet which then is then used as the index for the key which is then replaced correspondingly in the ciphertext string.

The problem here is that on the first letter of the plaintext is iterating over every letter of the alphabet and it correctly finds and replaces the correct letter at the correct index which is N but it replaces the plaintext too and not just the ciphertext.

I am not trying to solve this problem just trying to understand what is going on as I have already debugged this. What happens next is that the first letter keeps iterating through the rest of the alphabet and until it finds N and then replaces that with correct letter from the alphabet but with the plaintext too.

**

The Questions

Why does plaintext pick up letters when I only set the ciphertext to do so?

To answer the question

Why does plaintext pick up letters when I only set the ciphertext to do so?

On the first line of your function ciphertext_generator it says string ciphertext = plaintext; This lines states that the ciphertext variable will be equal to the plaintext variable meaning the memory of ciphertext points to the same memory as plaintext so any changes made to ciphertext will be applied to plaintext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM