如何限制用户在C中输入大于指定长度的字符和整数?

如何限制用户在C中输入大于指定长度的字符和整数?,c,stdio,C,Stdio,我正在尝试在OTP一次性Pad加密上创建一个C程序。程序从用户处获取要加密的字符串。然后,它必须在获取整数中的密钥之前询问密钥的长度。因为我希望程序100%正确,所以我对键的长度进行了限制。我们知道,在OTP中,密钥的长度必须是整数,不能超过文本的长度。那么,我们如何实现这样一个过滤器呢?我尝试过创建代码,但不起作用 代码如下: //An Under-Construction program to implement OTP (One time Pad) encryption #inc

我正在尝试在OTP一次性Pad加密上创建一个C程序。程序从用户处获取要加密的字符串。然后,它必须在获取整数中的密钥之前询问密钥的长度。因为我希望程序100%正确,所以我对键的长度进行了限制。我们知道,在OTP中,密钥的长度必须是整数,不能超过文本的长度。那么,我们如何实现这样一个过滤器呢?我尝试过创建代码,但不起作用

代码如下:

//An Under-Construction program to implement OTP (One time Pad) encryption     
#include "stdio.h"
#include "cstring"
#include "ctype.h"
int main()
{
    char str[10000], key[10000];
    printf("Enter the string:- ");
    scanf("%[^\n]", str); //stops scanning when user presses return

    int len /* stores the number of digits of the OTP key*/, isalphabet=0;
        do
        {
            printf("What is the length of the key you are entering?:-");
            scanf("%d", &len);

            if( isalpha(len) ) // Checks if len is a character
            {
                isalphabet=NULL; //isalphabet becomes NULL if it is a character and not an integer
            }


        } while (len > strlen(str) || isalphabet == NULL); //reiterate the loop until the conditions are satisfied

        for(int i = 0; i < len; i++)
    {
        printf("%dth digit of Key= ", i+1);
        scanf("%d", &key[i]);
    }
    return(0);  
}
我希望程序在扫描'len'时只从用户处获取整数值,并且'len'的值不应超过要加密的字符串的长度,如果不满足这些条件,则重复循环。有人能指出我的错误并给出解决方案吗?另外,请向我解释代码不能正常工作的原因。

检查scanf%d,len;有什么地方不见了吗?也初始化len

更新:- 抱歉迟到了, 好的,事实上你需要纠正的事情很少,对不起,我没有在第一篇文章中指出它们

1更正标题包含分隔符。使用而不是stdio.h

2初始化len=0

3阅读后冲洗标准DIN。如果您确实输入了一个字符,scanf%d,&len将不会清除/读取它。因此,由于stdin中的字符阻塞,您将陷入无限循环。最后一个for循环也是如此。幸运的是,在这里您不会被卡住,但是循环将提前结束

4 isalphabet=NULL,isalphabet是int,NULL基本上是一个带有0x0值的空指针,应该将0赋值给int

5所以你进入循环时,isalphabet=0,数字不是字母表,你没有触摸isalphabet,while循环结束时,isalphabet==NULL,这里它可能会作为true运行,while循环再次启动。那么,什么时候将循环中断条件设置为isalphabet

6为什么在c代码中使用cstring?为什么不使用string.h,但这更多是个人的选择:

7将len>strlen更正为len>=strlen

我编辑了一点,检查它是否有效

//An Under-Construction program to implement OTP (One time Pad) encryption     
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char str[10000], key[10000];
    printf("Enter the string:- ");
    scanf("%[^\n]", str); //stops scanning when user presses return

    int len /* stores the number of digits of the OTP key*/, isalphabet=0;
        do
        {
            printf("What is the length of the key you are entering?:-");
            scanf("%d", &len);
            fflush(stdin);

            if( isalpha(len) ) // Checks if len is a character
            {
                isalphabet=1; //isalphabet becomes NULL if it is a character and not an integer
            }


        } while (len >= strlen(str) || isalphabet == 1); //reiterate the loop until the conditions are satisfied

        int i;
        for(i = 0; i < len; i++)
    {
        printf("%dth digit of Key= ", i+1);
        scanf("%d", &key[i]);
    }
    return(0);  
}

首先,扫描%d,len;应该是

scanf("%d", &len);
strlenstr返回长度,比较应为

len > (strlen(str) -1)
if是否为alpha的比较可以在while循环中完成。 检查此代码

#include "stdio.h"
#include "string.h"
#include "ctype.h"

#define TRUE 1
#define FALSE 0

int main()
{
    char str[10000];
    char key[10000];

    printf("Enter the string:- ");
    scanf("%[^\n]", str); //stops scanning when user presses return

    int len = 0;
    int isalphabet= TRUE;
    int apply = 1;
    do
    {
        printf("What is the length of the key you are entering?:-");
        scanf("%d", &len);

        isalphabet= isalpha(len);
        apply = (len > (strlen(str) -1)) ;

    } while (!apply || isalphabet); //reiterate the loop until the conditions are satisfied

    int i;
    for(i = 0; i < len; i++)
    {
        printf("%dth digit of Key= ", i+1);
        scanf("%c\n", &key[i]);
    }

    for(i = 0; i < len; i++){
        printf("key[%d] = %c\n", i, key[i]);
    }
    return(0);  
}

你的意思是钥匙不能短于文字吗?否则您必须重复密钥,这会使加密容易破解。@Arlene请解释您遇到的具体问题。@NicholasWilson:-理想情况下,要使密码100%不可破解,密钥应该与文本一样长。由于这在所有情况下都是不可能的,所以密钥应该尽可能长,但在任何情况下都不能超过要加密的文本的长度。@Armin:编译并运行代码,您就会明白我的意思。当它询问您输入的密钥长度是多少?:-,请输入任何字符,例如:-“a”。你会发现这个程序疯了。这就是我试图创建过滤器的原因,我需要帮助。谢谢你编辑的代码。我编译并运行了代码,但“len”接受的长度大于字符串的长度。此外,如果顽皮的用户在中输入任何字符,例如:-“a”作为“len”的值,则程序将进入不适当的状态。我已通过将scanf%d、len替换为scanf%d和len来编辑代码,但它仍然不起作用。谢谢亲爱的朋友。代码实际上是有效的!!似乎有些成员不理解我的问题或没有答案,并将其标记为2。但是你费了好大劲才找到解决办法。这是一个很大的帮助。再一次,非常感谢。乐于助人,永不停止尝试,任何问题都不够愚蠢: