Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cs50 pset2凯撒——密文错误_C_Cs50 - Fatal编程技术网

cs50 pset2凯撒——密文错误

cs50 pset2凯撒——密文错误,c,cs50,C,Cs50,我已经解决了验证问题,但现在我只得到一个字符串作为密文,我找不到问题。这可能是我错过的一些小东西,但任何帮助都是感激的。 顶部是我的终端窗口,底部是我的代码 ~/caesar/ $ make caesar clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow caesar.c -lcrypt -lcs50 -

我已经解决了验证问题,但现在我只得到一个字符串作为密文,我找不到问题。这可能是我错过的一些小东西,但任何帮助都是感激的。 顶部是我的终端窗口,底部是我的代码

~/caesar/ $ make caesar
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    caesar.c  -lcrypt -lcs50 -lm -o caesar
~/caesar/ $ ./caesar 12
plaintext: "world, say hello!"
ciphertext: aaaaaaaaaaaaaaaaaaa 




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

int main(int argc, string argv[])
{
    if(argc == 2)
    {
        int n = strlen(argv[1]);
        int i = 0;
        for(i = 0; i < n; i++)
        {
            if(!isdigit(argv[1][i]))
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }
        int key = atoi(argv[1]);
         string text = get_string("plaintext: ");
            printf("ciphertext: ");
            int l = 0;
            int t = strlen(text);
        for(l = 0; l < t; l++)
         {
            if(isupper(text[i]))
            {
                printf("%c", (((text[i] - 'A') + key) % 26) + 'A');
            }

            else if(islower(text[i]))
            {
                printf("%c", (((text[i] - 'a') + key) % 26) + 'a');
            }

            else
            {
                printf("%c", text[i]);
            }
        }
    }
     else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    printf("\n");
    return 0;
}
~/caesar/$make caesar
clang-ggdb3-O0-std=c11-Wall-Werror-Wextra-Wno符号比较-Wno未使用参数-Wno未使用变量-Wshadow caesar.c-lcrypt-lcs50-lm-o caesar
~/caesar/$/caesar 12
明文:“世界,打招呼!”
密文:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符串argv[])
{
如果(argc==2)
{
int n=strlen(argv[1]);
int i=0;
对于(i=0;i
您总是在测试文本[i]而不增加i,您应该测试文本[l]

int main(int argc, string argv[])
{
    if(argc == 2)
    {
        int n = strlen(argv[1]);
        int i = 0;
        for(int l = 0; l < n; l++)
        {
            if(!isdigit(argv[1][l]))
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }
        int key = atoi(argv[1]);
         string text = get_string("plaintext: ");
            printf("ciphertext: ");
            int t = strlen(text);
        for(int l = 0; l < t; l++)
         {
            if(isupper(text[l]))
            {
                printf("%c", (((text[l] - 'A') + key) % 26) + 'A');
            }

            else if(islower(text[l]))
            {
                printf("%c", (((text[l] - 'a') + key) % 26) + 'a');
            }

            else
            {
                printf("%c", text[l]);
            }
        }
    }
     else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    printf("\n");
    return 0;
}
intmain(intargc,字符串argv[])
{
如果(argc==2)
{
int n=strlen(argv[1]);
int i=0;
对于(int l=0;l
此代码更干净。

\code>#包括
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

  int main(int argc, string argv[])
  {
   
    if (argc == 2)
    {
            int digits = strlen(argv[1]);
            bool isnum = true;
            for (int i = 0; i < digits; i++)
            {
                    if (isdigit(argv[1][i]) == 0)
                    {
                            isnum = false;
                            break;
                    }

            }//checks weather the give input of argument are correct
            if (isnum == true)
            {
                    string text = get_string("Enter the text: ");
                    char textalpha[25];
                    char ciphertext[strlen(text)];
                    int a = atoi(argv[1]);   //converts argv to int
                    for (int y = 0; y <= 25 ; y++)
                    {
                        textalpha[y] = (char)(y + 97);
        //              creates an array of alphabets with index start from 0 to 25
                    }
        //ciphertext[w] = (textalpha[w] +a) % 26; This the main forluma for rotating the alphabets
        
                    for (int w = 0; w <= strlen(text); w++)
                    {
                            if (isalpha(text[w]))
                            {
                                    if (isupper(text[w]))
                                    {
                                            ciphertext[w] = textalpha[((((int)text[w] - 65) + a) % 26)];
                                            //printf("%c",toupper(ciphertext[w]));
                                            ciphertext[w] = toupper(ciphertext[w]);
                                    }
                                    else if (islower(text[w]))
                                    {
                                            ciphertext[w] = textalpha[((((int)text[w] - 97) + a) % 26)];
                                            //printf("%c",tolower(ciphertext[w]));
                                            ciphertext[w] = tolower(ciphertext[w]);
                                    }
                            }
                            else
                            {
                                    //printf("%c",text[w]);
                                    ciphertext[w] = text[w];
                            }
        
                    }
        
                    printf("ciphertext: %s\n", ciphertext);//prints the cipher text
                    return 0;
            }
            else
            {
                    printf("Usage: ./caesar key\n");
        //          prints if the entered arguments are not in this form
                    return 1;
            }
    }
    else
    {
            printf("Please provide us with 'one' argument.\n");
    //      prints if the arguments entered are more or less than two.
            return 1;
    }
}
#包括 #包括 #包括 #包括 int main(int argc,字符串argv[]) { 如果(argc==2) { 整数位数=strlen(argv[1]); bool-isnum=true; 对于(int i=0;i对于(int y=0;y),这个答案只是一堆没有任何解释的代码。你能解释一下代码是如何解决这个问题的吗?