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

标签 c stdio

我正在尝试创建一个基于 OTP (One Time Pad) 加密的 C 程序。该程序从用户处获取要加密的字符串。然后它必须在获取 key 之前询问 key 的长度(整数)。由于我希望程序 100% 正确,因此我对 key 的长度施加了限制。众所周知,在 OTP 中, key 的长度必须是整数,不能超过文本的长度。那么,我们如何实现这样一个过滤器呢?我尝试过创建代码,但它不起作用。

代码如下:

//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”的值不应超过要加密的字符串的长度,如果满足这些条件,则重复循环t 满意。有人可以指出我的错误并展示解决方案吗?另外,请向我解释一下代码无法正常工作的原因。

最佳答案

检查scanf("%d", len);是否有某个地方 &丢失的?还初始化len

更新:- 对不起,来晚了, 好吧,实际上有一些事情你需要纠正,抱歉我没有在第一篇文章中指出它们。

1) 更正 header 包含分隔符。使用<stdio.h>而不是"stdio.h"

2) 初始化len = 0

3) 读完后刷新标准输入。如果您确实输入一个字符,则 scanf(%d,&len)不会清除它/读取它。因此,由于字符卡在标准输入中,您将陷入无限循环。最后一个 for 循环也是如此。幸运的是,你不会被卡住,但循环会提前结束。

4) isalphabet=NULL , isalphabet 是 int,NULL 基本上是一个值为 0x0 的 void 指针,您应该将 0 分配给 int。

5) 所以你进入循环时 isalphabet=0,数字不是字母表,你没有触及 isalphabet,while 循环结束,isalphabet==NULL ,这里也许它会作为 true 运行,并且 while 循环再次开始。那么循环中断条件何时设置为 isalphabet

6) 另外为什么要使用 cstring在c代码中?为什么不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);  
}

关于c - 我们如何限制用户在 C 中输入字符和大于指定长度的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15987237/

相关文章:

c - 链表头地址变化 C

c - 运算符优先级问题

c - 我想问一下我失败的代码

c - string FILE stdio 兼容吗?

c - C 中的标准库和彩色输出

c - 使用 isdigit() 和 isalpha() 命令的最简单方法是什么?

java - C 与 Java 原始转换和表达式

c - 为什么 scanf 可以读取超过 1024 个字符,而 stdin 流缓冲区只有 1024 个字节?

c++ - 库 'stdio' 与 'iostream' 的速度和稳定性

java - 线程 "main"java.lang.UnsatisfiedLinkError 3 中的异常