c - 不接受用户输入的其他选项

标签 c caesar-cipher

我必须在 C 中做凯撒密码。加密和解密取决于 key 和密码(最多 4 个字符)加上移动方向,这三个都是用户给定的。 在我的代码中,它只需要向左,当我向右输入时,它会再次重新询问方向。我不知道出了什么问题。

这是我的代码:

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

char pass[4];
char shiftdirection[6];
char left[6] = "left";
char right[6] = "right";
char ch;
int i, key;

void readline (){ //cette fonction prend de l'utilisateur le password et la clé
    do{
    printf("Please enter the pass, max 4 characters: \n");
    gets(pass);
    }while((strlen(pass)>4) || (strlen(pass)<4)); //get the password from th
    printf("Please enter the key: \n");
    scanf("%d",&key);
}

//cette fonction utilisée pour chiffrer
void encrypt(){
    printf("Enter the direction: \n");
    scanf("%s", &shiftdirection);
    while ((strcmp(shiftdirection,left)==1) || (strcmp(shiftdirection,right)==1)) { // demander de l'utilisateur la direction de décalage

        printf("Enter the direction: \n");
        scanf("%s", &shiftdirection);
    }
    if(strcmp(shiftdirection,left)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
    for( i = 0; pass[i] != '\0'; i++){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch + key);
            if(ch > 'z'){
                ch = (char) (ch - 'z' + 'a' - 1);
            }
            pass[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch + key);
            if(ch > 'Z'){
                ch = (char) (ch - 'Z' + 'A' - 1);
                }
            pass[i] = ch;
            }
        }
    }
   else if(strcmp(shiftdirection,right)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
        for( i = strlen(pass) - 1; i >= 0; --i){
            ch = pass[i];
            if(ch >= 'a' && ch <= 'z'){
                ch = (char) (ch + key);
                if(ch > 'z'){
                    ch = (char) (ch - 'z' + 'a' - 1);
                }
                pass[i] = ch;
            }
            else if(ch >= 'A' && ch <= 'Z'){
                ch = (char) (ch + key);
                if(ch > 'Z'){
                    ch = (char) (ch - 'Z' + 'A' - 1);
                }
                pass[i] = ch;
            }
        }
    }
    printf("Encrypted Password is %s", pass); // le password chiffré
}

//cette fonction utilisée pour déchiffrer
void decrypt(){
    for(i = 0 ; pass[i] != '\0' ; ++i){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch - key);
            if(ch < 'a') {
                ch = (char) (ch + 'z' - 'a' + 1);
            }
            pass[i] = ch;
        } else if (ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch - key);
            if(ch < 'A'){
                ch = (char) (ch + 'Z' - 'A' + 1);
            }
            pass[i] = ch;
        }
    }
    printf("\nDecrypted Password is %s", pass); //le password déchiffré
}
//main
int main() {
    readline();
    encrypt();
    decrypt();
    return 0;
}

INPUT AND OUTPUT

最佳答案

  1. 正如评论中提到的,您必须更改 char pass[4] -> char pass[5]考虑\0\n从按回车。
  2. 不要使用 gets .使用 fgets相反导致 gets是不安全的,不再是 C 的一部分
  3. 使用getchar()捕获剩余的 \0按回车键
  4. 更改scanfscanf_s为了更安全
  5. 你的 while ((strcmp(shiftdirection,left)==1) || (strcmp(shiftdirection,right)==1))检查条件之一是否为真然后进入。您需要检查这两个条件是否都不成立,因此您需要 &&而不是 ||

我稍微研究了一下您的代码并得出了以下结论。

代码:

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

char pass[5];
char shiftdirection[6];
char left[6] = "left";
char right[6] = "right";
char ch;
int i, key;

void readline (){ //cette fonction prend de l'utilisateur le password et la clé
    do{
    printf("Please enter the pass, max 4 characters: \n");
    fgets(pass, sizeof pass, stdin);
    }while((strlen(pass)>4) || (strlen(pass)<4)); //get the password from th
    printf("Please enter the key: \n");
    scanf_s("%d",&key);
    getchar();
}

//cette fonction utilisée pour chiffrer
void encrypt(){
    printf("Enter the direction: \n");
    fgets(shiftdirection, sizeof shiftdirection, stdin);
    shiftdirection[strcspn(shiftdirection, "\n\r")] = 0;
    while ((strcmp(shiftdirection,left)==1) && (strcmp(shiftdirection,right)==1)) { // demander de l'utilisateur la direction de décalage

        printf("Enter the direction: \n");
        fgets(shiftdirection, sizeof shiftdirection, stdin);
        shiftdirection[strcspn(shiftdirection, "\n\r")] = 0;
    }
    if(strcmp(shiftdirection,left)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
    for( i = 0; pass[i] != '\0'; i++){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch + key);
            if(ch > 'z'){
                ch = (char) (ch - 'z' + 'a' - 1);
            }
            pass[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch + key);
            if(ch > 'Z'){
                ch = (char) (ch - 'Z' + 'A' - 1);
                }
            pass[i] = ch;
            }
        }
    }
   else if(strcmp(shiftdirection,right)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
        for( i = strlen(pass) - 1; i >= 0; --i){
            ch =pass[i];
            if(ch >= 'a' && ch <= 'z'){
                ch = (char) (ch + key);
                if(ch > 'z'){
                    ch = (char) (ch - 'z' + 'a' - 1);
                }
                pass[i] = ch;
            }
            else if(ch >= 'A' && ch <= 'Z'){
                ch = (char) (ch + key);
                if(ch > 'Z'){
                    ch = (char) (ch - 'Z' + 'A' - 1);
                }
                pass[i] = ch;
            }
        }
    }
    printf("Encrypted Password is %s", pass); // le password chiffré
}

//cette fonction utilisée pour déchiffrer
void decrypt(){
    for(i = 0 ; pass[i] != '\0' ; ++i){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch - key);
            if(ch < 'a') {
                ch = (char) (ch + 'z' - 'a' + 1);
            }
            pass[i] = ch;
        } else if (ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch - key);
            if(ch < 'A'){
                ch = (char) (ch + 'Z' - 'A' + 1);
            }
            pass[i] = ch;
        }
    }
    printf("\nDecrypted Password is %s", pass); //le password déchiffré
}
//main
int main() {
    readline();
    encrypt();
    decrypt();
    system("pause");
    return 0;
}

注意 shiftdirection[strcspn(shiftdirection, "\n\r")] = 0;检查剩余的字符串 \n所以getchar()可能会被替换。

希望这就是你要找的

编辑您的while((strlen(pass)>4) || (strlen(pass)<4));基本上是 while(strlen(pass)!=4);这意味着 pass应始终为 4 个字符长。是这样吗?或者用户也可以输入小于 4?

更新:

检查了

while ((strcmp(shiftdirection,left)==1) && strcmp(shiftdirection,right)==1))条件再次,因为它是错误的。

改成 while ((strcmp(shiftdirection,left)!=0) && (strcmp(shiftdirection,right)!=0)) .

strcmp(shiftdirection,left)strcmp(shiftdirection,right)不是 leftright因此,值为 -1不是 1 .

关于c - 不接受用户输入的其他选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43856033/

相关文章:

c - sizeof(struct) 对于不同的编译器是不同的

javascript - 如何在 Javascript 中处理凯撒密码的移位?

java - 带有/模运算的凯撒密码

c - 如何在以管理员身份启动的应用程序中查找用户名

c - 得到一个奇怪的指针错误,虽然没有使用指针

c - 用户空间程序中的 "volatile"是否表示存在错误?

ruby - 使用凯撒密码时如何保留字符大小写

JavaScript动态创建对象未定义

C - 凯撒密码 [将代码移至函数]

C 程序输出错误