首页 > 解决方案 > 根据密钥更改字母(加密)

问题描述

我正在尝试通过具有给定密钥(变量 argv [1])的密文来加密来自用户的输入(变量文本)。其中 A 替换为键的第一个字母,B 替换为键的第二个字母,依此类推。

例如,如果我在终端中输入: ~./test YTNSHKVEFXRBAUQZCLWDMIPGJO

并且用户输入以下文本text: OVERFLOW

输出是:ciphertext: QIHLKBQP

我现在的问题是我的功能不适用于以下用户输入:The quick brown fox jumps over the lazy dog使用键./test DWUSXNPQKEGCZFJBTLYROHIAVM

我得到什么: ciphertext: Rqx tokug wljif nja eozby jhxl rqx c

我想得到什么: ciphertext: Rqx tokug wljif nja eozby jhxl rqx cdmv sjp

为什么我的代码在到达 c 后停止替换字母并停止添加"... cdmw sjp"?

我目前的代码是:

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



int l = 97;
int j = 65;
int difference;
int differencee;
int k=0,b, c;



int main(int argc, string argv[])
{
    string text = get_string("Plaintext: ");     // Asking for the text-input


    printf("ciphertext: ");
    for(i = 0; i < strlen(text); i++, j++, l++) //loop for the given text length
    {
        difference = argv[1][i] - j;
        differencee = argv[1][i] - l;

            if(j==text[k] && text[k]<91 && text[k]>64) // Check for UPPERCASE LETTERS and change according to key
            {
                b = text[k] + difference;

                k++;
                printf("%c",toupper(b));
                i = 0;
                j = 64 +1;
                l = 96 + 1;
            }
            if(l==text[k] && text[k]<123 && text[k]>96)   //Check for LOWERCASE LETTERS and change according to key
            {
                c = text[k] + differencee;

                k++;
                printf("%c",tolower(c));
                i = 0;
                l = 96 + 1;
                j = 64 + 1;
            }
            if(isalpha(text[k]) == 0 && text[k] != '\0') //If not alphabet, then do nothing
            {
                printf("%c",text[k]);
                k++;
                i = 0;
                
            }
    }
    printf("\n");
}

标签: arrayscstringnested-loopscs50

解决方案


您可以简单地解决您的算法。无需额外的计算和变量。

检查您的示例并将其更改以涵盖您的工作:输入和输出如您所愿。

#include <math.h>
#include <ctype.h>
#include <string.h>

int main()
{
    char text[64] = "The quick brown fox jumps over the lazy dog\0";     // Asking for the text-input

    char argv[32] = "DWUSXNPQKEGCZFJBTLYROHIAVM";
    int len = strlen(argv);
    int i=0;
    char temp = 0;
    int index = 0;
    printf("strlen(text): %d strlen(argv) = %d\n ",strlen(text),len);
    printf("ciphertext: ");
    for(i = 0; i < strlen(text); i++) //loop for the given text length
    {   
        if(isalpha(text[i]) == 0 && text[i] != '\0') //If not alphabet, then do nothing
        {
            printf("%c",text[i]);
            continue;
        }
            
        temp = text[i];
        
        if(temp >= 'A' && temp <= 'Z') // Check for UPPERCASE LETTERS and change according to key
        {
            temp +=32;
        }
            
        if(temp >= 'a' && temp <= 'z')   //Check for LOWERCASE LETTERS and change according to key
        {
            index = (temp - 'a')%len;
            printf("%c",argv[index]);
        }
            
    }
    printf("\n");
}

已编辑: 在线演示


推荐阅读