首页 > 解决方案 > 文本加密仅适用于小写,大写字母不变

问题描述

我正在处理 pset,我需要一些帮助。该程序的目的是获取命令行参数并将其用作密钥来加密用户输入的一些明文,并将其移动给定的字母数。大写字母应保持大写,小写 - 小写。所有其他符号应保持不变。我的代码只加密小写字母而不加密大写字母。请帮我找出原因。

 void encryption(string plaintext, int k, string ciphertext);
    int main(int argc, string argv[])
    {
        string plaintext;
        int n = strlen(plaintext);
        char ciphertext[n+1]; //the lenght of the plaintext+ 1 extra char which i
        int k = atoi(argv[1]); //Convert string into an integer, i.e parsing
    
    
        if (argc != 2)
        {
           printf("Usage: ./caesar key\n");
            return 1;
        }
        else
        {
    
     
            for (int i = 0, m = strlen(argv[1]); i < m; i++)
        {
        if (isdigit(argv[1][i]))
          {
     plaintext = get_string("Plaintext:");
     encryption(plaintext, k, ciphertext); //calling the encryption function
       printf("Ciphertext: %s\n", ciphertext);
       return 0;
    
          }
    
       else
       {
          printf("Usage: ./caesar key\n");
            return 1;
       }
    
            }
        }
    }
    void encryption(string plaintext, int k, string ciphertext)
    {
    
        for (int i = 0, n = strlen(plaintext); i < n; i++)
        {
            if (isupper(plaintext[i]))
            {
                int  pi = plaintext[i] - 65;
                char ci = ((pi + k) % 26) + 65;
                ciphertext[i] = ci;
            }
    
            if (islower(plaintext[i]))
            {
                int pi1 = plaintext[i] - 97;
                char ci1 = ((pi1 + k) % 26) + 97;
                ciphertext[i] = ci1;
            }
            else
            {
                ciphertext[i] = plaintext[i];
            }
    
        }
    }

标签: c

解决方案


推荐阅读