L oop изменяющаяся строка, которая не находится внутри l oop? - PullRequest
0 голосов
/ 02 февраля 2020

Я делаю программу на C, которая получает строку ogplaintext, ввод с клавиатуры и выводит зашифрованный текст. Однако, представленный здесь l oop, вместо того, чтобы только изменять переменную зашифрованного текста, также меняет ogplaintext, что для меня не имеет смысла, так как ogplaintext нигде в l oop нет. (Ошибка?) Возникает сразу после операции "ciphertext [j] = argv [1] [i]", где argv [1] - это ключ, введенный пользователем. Мысли об этом?

string ciphertext = ogplaintext;
int j = 0;
printf("ogplaintext: %s ; initiating ciphertext conversion \n", ogplaintext);
// convert j'th digit of ciphertext in key
while(j < strlen(ciphertext))
{
    for(int i = 0; i < 25; i++)
    {
        if(i == editplaintext[j])
        {
            ciphertext[j] = argv[1][i];
            j++;
            printf("after ciphertext character change ogplaintext: %s\n", ogplaintext);
        }
        else if(editplaintext[j] == 27)
        {
            j++;
        }
    }
}

Я вставлю сюда весь код, если он поможет:

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

int main(int argc, string argv[])
{
    if(argc < 2)
    {
        printf("Usage: ./substitution key");
        return 1;
    }
    else
    {
        if(strlen(argv[1]) != 26)
        {
            printf("Key must contain 26 characters.");
            return 1;
        }
        for(int i = 0; i < 25; i++)
        {
            if(argv[1][i] < 65 || (argv[1][i] > 90 && argv[1][i] < 97) || argv[1][i] > 122)
            {
                printf("Usage: ./substitution key");
                return 1;
            }
        }
    }
    string ogplaintext = get_string("plaintext: ");
    string plaintext = ogplaintext;
    int editplaintext[strlen(ogplaintext)];
    for(int i = 0; i < strlen(ogplaintext); i++)
    {
        // if oglaintext[i] is uppercase
        if(ogplaintext[i] >= 65 && ogplaintext[i] <= 90)
        {
            // convert in digit from 0 to 25
            editplaintext[i] = ogplaintext[i] - 65;
        }
        // if ogplaintext[i] is lowercase
        else if(ogplaintext[i] >= 97 && ogplaintext[i] <= 122)
        {
            // convert in digit from 0 to 25
            editplaintext[i] = ogplaintext[i] - 97;
        }
        // if ogplaintext[i] is not alphabetical
        else
        {
            editplaintext[i] = 27;
        }

    }

    string ciphertext = ogplaintext;
    int j = 0;
    printf("ogplaintext: %s ; initiating ciphertext conversion \n", ogplaintext);
    // convert j'th digit of ciphertext in key
    while(j < strlen(ciphertext))
    {
        for(int i = 0; i < 25; i++)
        {
            if(i == editplaintext[j])
            {
                ciphertext[j] = argv[1][i];
                j++;
                printf("after ciphertext character change ogplaintext: %s\n", ogplaintext);
            }
            else if(editplaintext[j] == 27)
            {
                j++;
            }
        }
    }
    printf("ogplaintext: %s ; finalized ciphertext conversion \n", ogplaintext);
    for( int i = 0; i < strlen(ciphertext); i++)
    {
        // if ith character of ogplaintext is uppercase and ith character of ciphertext is lowercase
        if((ogplaintext[i] >= 65 && ogplaintext[i] <= 90) && (ciphertext[i] >))
        {
            ciphertext[i] = (ciphertext[i] - 32);
        }
        // else if ith character of ogplaintext is lowercase and ith character of ciphertext is uppercase
        else if((ogplaintext[i] >= 97 && ogplaintext[i] <= 122) && (ciphertext[i] >= 65 && ciphertext[i] <= 90))
        {
            ciphertext[i] = (ciphertext[i] + 32);
        }
        else
        {
            printf("c");
        }
    }
    printf("ciphertext: %s", ciphertext);    
}

1 Ответ

0 голосов
/ 02 февраля 2020

Используйте strcpy (зашифрованный текст, ogplaintext) вместо =.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...