凯撒密码C语言为降低不上工作 [英] Caesar cipher in C working for lower not upper

查看:174
本文介绍了凯撒密码C语言为降低不上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

密码适用于islower判断部分但不是isupper部分。举例来说,如果我给的3键,输入我喜欢馅饼!来进行加密,我得到 0 olnh SLH !! 我也试过 HELLO 并获得 NKRRU 。该isupper部分也只返回字母标点来代替。我也就是为什么被改变了原始消息相匹配的密码信息还没有想出。

 的#include<&stdio.h中GT;
#包括LT&;&cs50.h GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&文件ctype.h GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,ARGV串[])
{
    / *
    在获取命令行从用户密钥
    得到用户的明文
    使用密钥加密的文字:C [i] =(P [I] + k)的26%
    打印加密消息
    * /
    字符串消息,密码;
    INT关键;    //命令行,如果用户没有输入2个参数返回1,并要求有效
    //加密密钥,然后重新运行。
    如果(的argc!= 2)
        {
        的printf(请输入一个有效的加密密钥,然后重新运行程序\\ n);
        返回1;
        }
    其他
        {
        键=的atoi(ARGV [1]);
        }
    的printf(请输入您要对消息进行加密\\ n);
    消息= GetString的();
    密码=消息;
    INT长度= strlen的(消息);    的for(int i = 0; I<长度;我+ +)
        {
        如果(因而isalpha(消息[I]))
            {
            如果(isupper(消息[I]))
                {
                密码由[i] =(消息[I] - 'A'+键)%26 +'A';
                }
            其他(islower判断(消息[I]));
                {
                密码由[i] =(消息[I] - 'A'+键)%26 +'A';
                }
            }
        否则继续; //消息[I]包含标点符号或空格
        }
         的printf(您的原始消息是.. \\ n);
         的printf(%S \\ n,邮件);
         的printf(加密的消息是... \\ n);
         的printf(%S \\ n,密码);
         返回0;
}


解决方案

错字和漏如果每@interjay。

修改

 其他(islower判断(消息[I]));

  // v
否则,如果(islower判断(消息[I]))
//或者干脆
否则//由于`消息[]`是一个字母,但不上

通过错误,当文本是大写的,无论是密码[I] =(消息[I] - 'A'... 密码由[i] =(消息[I] - '一'... 发生由于密码=信息,密码被涂抹两次。


有关缺少缓冲@keshlam点是一个显著的问题。但我不知道是什么类型的字符串是。这是某种C ++精简版的字符串?如果是的char * ,code可以使用密码=的strdup(消息);

 密码=的malloc(长+ 1);
如果(密码=== NULL)Handle_OutOfMemeory();
密码[长度] ='\\ 0';
的for(int i = 0; I<长度;我+ +)
  ...

Cipher works for islower portion but not isupper portion. For instance if I give a key of 3 and enter I like pie!! to be encrypted, I get O olnh slh!! I also tried HELLO and got NKRRU. The isupper portion is also returning punctuation instead of just letters. I also have not figured out why the original message is being altered to match the cipher message.

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

int main (int argc, string argv[])
{
    /*
    Get key from user at command line
    Get plaintext from user
    Use key to encipher text: c[i] = (p[i] + k)%26
    Print ciphered message
    */        
    string message, cipher;
    int key;

    // command line, if user doesn't enter 2 arguments return 1 and request a valid 
    //encryption key and rerun.
    if (argc != 2)
        {
        printf("Please enter a valid encryption key and rerun program.\n");
        return 1;
        }
    else
        {
        key = atoi(argv[1]);
        }


    printf("Enter the message you wish to encrypt.\n");
    message = GetString();
    cipher = message;
    int length = strlen(message);

    for ( int i = 0; i < length; i++)
        {
        if (isalpha(message[i]))
            {
            if (isupper(message[i]))
                {
                cipher[i] = (message[i] - 'A' + key) % 26 + 'A';
                }
            else (islower(message[i]));
                {
                cipher[i] = (message[i] - 'a' + key) % 26 + 'a';
                }
            }
        else continue; //message[i] contains punctuation or a space
        } 
         printf("Your original message was..\n");
         printf("%s\n", message);
         printf("The encrypted message is...\n");
         printf("%s\n", cipher);         
         return 0;            
}

解决方案

Typo and missing if per @interjay.

Change

else (islower(message[i]));

to

//                           v
else if (islower(message[i]))
// or simply 
else  // Since `message[]` is an alpha, but not upper

With the error, when text was uppercase, both cipher[i] = (message[i] - 'A' ... and cipher[i] = (message[i] - 'a' ... occurred. Given cipher = message, the cipher was applied twice.


@keshlam point about the missing buffer is a significant issue. But I wonder what type string is. Is this some sort of C++ lite string? If it is a char *, code could use cipher = strdup(message); or

cipher = malloc(length + 1);
if (cipher === NULL) Handle_OutOfMemeory();
cipher[length] = '\0';
for ( int i = 0; i < length; i++) 
  ...

这篇关于凯撒密码C语言为降低不上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆