【问题标题】:Mono alphabetic cipher doesnt work correctly单字母密码不能正常工作
【发布时间】:2016-11-23 14:34:29
【问题描述】:

这是我的代码。 C 中的替换密码。但我在这一行遇到错误:char *encryption (char cipher_text[]) { 此处不允许函数定义。我想可能是“主要”功能的地方不对。我该如何解决? 顺便说一句,我怎样才能为这段代码生成随机字母?非常感谢。

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


char *encryption (char cipher_text[]) {
            int i, val, j;
            printf("\n abcdefghijklmnopqrstuvwxyz \n");  

【问题讨论】:

  • 你不能嵌套函数..
  • 如果你想生成一个随机字母表,看看使用rand() 函数 - 你想生成一个随机数并将其修改为 26,然后添加 97,并存储变量在char。请参阅 asciitable.com/index/asciifull.gif 了解我们添加 97 的原因
  • srand((unsigned int) time(NULL) ); char key = rand( ) % 26 + 'a'; 我为随机生成字母添加了这一行,但出现错误:cipher_text[i]=key[j]; 下标值不是数组、指针或向量。我该如何解决?

标签: c encryption


【解决方案1】:

你不能在另一个函数中定义一个函数。加密在 main 中定义:/

【讨论】:

    【解决方案2】:

    在 C 中,您不能像以前那样在另一个函数中声明一个函数。 这是您将编译的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <ctype.h>
    #include <string.h>
    
    char *encryption (char []);
    void *decryption (char []);
    char alpha [26]={'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'};
    char key[26];
    
    
    char *encryption (char cipher_text[]) {
    int i, val, j;
    printf("enter the unique KEY of 26 character:");
    scanf("%s", key);
    printf("\n abcdefghijklmnopqrstuvwxyz \n");
    printf ("%s", key);
    
    for (i=0; i <strlen(cipher_text); i++) 
    {
        for (j=0; j<26; j++)
        {
            if (alpha[j]== cipher_text[i]) {
                cipher_text[i]=key[j];
                break;
            }
        }
    }
    
    printf ("your message enc: %s", cipher_text);
    return cipher_text; 
    }
    
    int main ()
    {
        int i, key, choice, flag=0;
        char *c_text, msg[255];
        printf("\n Enter plain text:");
        scanf ("%[^\n]", msg);
        encryption(msg);
        return 0;
    }
    

    如何生成随机字符回答here

    【讨论】:

    • 谢谢,但我仍然遇到错误(在 OS X 中使用 Xcode)char *cipher_text, msg[255];(不是 c_text btw)这一行说:未使用的变量密文。程序正在运行,但我只是输入了明文然后程序退出。 :/ 它没有加密。非常感谢。
    • 好吧,你必须从 main 调用函数。我刚刚将它添加到我发布的代码中。
    • 你确定你复制的代码完全正确吗?函数调用(encryption(msg);)几乎在最后一行?它对我有用。
    • 好的,现在可以工作了,但我收到了关于值的错误:/ 非常感谢。我会检查这个加密是真还是假。
    • 没问题。如果它回答了您的问题,您可以接受此答案。
    猜你喜欢
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多