Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
凯撒密码问题(C)_C_Encryption_Scanf - Fatal编程技术网

凯撒密码问题(C)

凯撒密码问题(C),c,encryption,scanf,C,Encryption,Scanf,此代码的目的是创建一个凯撒密码,该密码仅编码具有3个键的字母数字字符,其中第1个字母由第1个键递增,第2个字符由第2个键递增,第3个字符由第3个键递增,第4个字符由第1个键递增等。。。使用环绕(Z+1-->a),(a-1-->Z) 我已经完全完成了作业,唯一的问题是我的负回绕不起作用(a-1-->Z)。它要求输入键,接受输入,然后什么也不返回(但仍然允许用户键入并按enter键,而不返回任何结果)。这是我的密码: #include <stdio.h> #include <mat

此代码的目的是创建一个凯撒密码,该密码仅编码具有3个键的字母数字字符,其中第1个字母由第1个键递增,第2个字符由第2个键递增,第3个字符由第3个键递增,第4个字符由第1个键递增等。。。使用环绕(Z+1-->a),(a-1-->Z)

我已经完全完成了作业,唯一的问题是我的负回绕不起作用(a-1-->Z)。它要求输入键,接受输入,然后什么也不返回(但仍然允许用户键入并按enter键,而不返回任何结果)。这是我的密码:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

char sentence[101] = { '\0' };
char alphabet[52]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int locateletter(char tolocate){
    int i=0;
    for(i=0; i<52; i++){
            if(alphabet[i] == tolocate)
                    return i;
    }
}

int main(void){
    int key1 = 0;
    int key2 = 0;
    int key3 = 0;
    printf("Sentence: ");
    scanf("%101[^\n]", sentence);
    if( sentence[100] != '\0' ){
        printf("You entered more than 100 characters. Block Caesar Cipher is exiting. Goodbye.\n");
        exit;
    }else{
        printf("Keys: ");

        scanf("%d %d %d", &key1, &key2, &key3);
        int i=0;
        for(i=0; i<100; i=i+3){
                if(isalpha(sentence[i])){
                        int position = locateletter(sentence[i]);
                        while((position+key1)>51){
                                key1 = position+key1-52;
                                position=0;
                        }
                        while((position+key1)<0){
                                key1 = key1+position+52;
                                position=0;
                        }
                        sentence[i] = alphabet[position+key1];
                }
        }
        int k=1;
        for(k=1; k<100; k=k+3){
                if(isalpha(sentence[k])){
                        int position = locateletter(sentence[k]);
                        while((position+key2)>51){
                                key2 = position+key2-52;
                                position=0;
                        }
                        while((position+key2)<0){
                                key1 = key2+position+52;
                                position=0;
                        }
                        sentence[k] = alphabet[position+key2];
                }
        }
        int t=2;
        for(t=2; t<100; t=t+3){
                if(isalpha(sentence[t])){
                        int position = locateletter(sentence[t]);
                        while((position+key3)>51){
                                key3 = position+key3-52;
                                position=0;
                        }
                        while((position+key3)<0){
                                key1 = key3+position+52;
                                position=0;
                        }
                        sentence[t] = alphabet[position+key3];
                }
        }
        printf("Cipher: %s\nDone.\n", sentence);

    }
    return 0;
}
#包括
#包括
#包括
#包括
字符句子[101]={'\0'};
[52][52][52]手手手手手字母字母表[52][52][52][52]手手手手手手手手手手手字母字母字母表[52][52][52][52][[52][[[52][[[52]、、'a“a”,“b”,“b”,“b”,“b”,“b”,“b”,“b”,“a”,“我”,“我”,“b”,“我”,“我”,“我”,“b”,“我”,“我”,“我,”b”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“我”,“","u","v","w","x","y","z";;
int LOCATELETER(字符定位){
int i=0;

对于(i=0;i我发现了错误。我的反向环绕while循环将值分配给key1,而不是key2/3