Vigenere Cypher-代码无法正常运行

时间:2019-02-17 02:55:24

标签: arrays encryption cs50 vigenere sc

我试图弄清楚为什么我的代码没有在z上正确迭代。我想每隔1 z进行一次迭代,但似乎有点随机。有人对我该如何做有任何建议吗?如果我还有其他地方做错了,请告诉我。我想我真的很亲近。我也很好奇如何阻止空格/其他字符被加密。

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int shift(char c);
int main(int argc, string argv[])
{
// Check to see if two arguments are enterted at launch 
int cipher = 0;
 if (argc != 2)
 {
    // If not return error & return 0
    printf("Usage: ./vigenere keyword \n");
    return 1;
 }
 else
 {
    int strlength = strlen(argv[1]);
    // Iterates through characters in second argument (key), checking to see 
    if they are digits
    for (int k = 0; k < strlength; k++)
    {
        if (isdigit(argv[1][k]))
        {
            // If not return error & return 1
            printf("Usage: ./vigenere keyword\n");
            return 2;
        }
     }
        //char *c =argv[1];
        string plaintext = get_string("Plaintext: ");
        int len = (int) strlen(plaintext);
        //int b = atoi(c);
        //char code[len];
        //strcpy (code, plaintext);
         int z=0;
         for (int j = 0; j < len; j++)   
         {

                int key = shift(argv[1][z]);
             //printf("%i",z);
              if (isupper(argv[1][z]))
                {
                  //printf("theory\n");
                  cipher = ((((plaintext[j] - 'A') + key) + 'A'));
                  //cipher = ((((plaintext[j] - 'A') + key) % 26) + 'A');
                  //printf("%c", (((plaintext[j] - 'A') + key) % 26) + 'A');
                 //printf("%i",z);


                  printf("%c",cipher);

                 }

                 if (islower(argv[1][z]))
                    {

                    //printf("theory\n");  
                    cipher = (((plaintext[j] - 'a') + key) + 'a');


                     //printf("%i",z);

                      printf("%c",cipher);  


                  }
                  //z++;     
                 //else if (!isalpha(plaintext[j]))
                    //{
                        //printf("%c", plaintext[j]);  
                     //}

                  if (z > strlen(argv[1])-1)
                 {
                          z=0;
                 }

                /* else 
                 {
                 z++;
                 }

                 else
                 {
                 z++;
                 }
                 */
                 //z++;
              }



            printf("\n");

             }
}
 int shift(char c)
 {
    int i = c;
    if (i <= 'Z' && i >= 'A')
    {
       return ((i - 'A') % 26);
    }
    else
    {
        return ((i - 'a') % 26);
    }

 }

0 个答案:

没有答案