Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 想做凯撒';s密码,但无法';不要更改最后两个字符_C_Caesar Cipher - Fatal编程技术网

C 想做凯撒';s密码,但无法';不要更改最后两个字符

C 想做凯撒';s密码,但无法';不要更改最后两个字符,c,caesar-cipher,C,Caesar Cipher,我为凯撒的密码写了一个密码,这个密码有效,除了我不能加密超过8个字母,我也不能处理空格。它显示“>>”此符号,而不是空格。另外,我想在代码的第二个函数中进行二进制搜索,但我不知道我是否已经这样做了 #include <stdio.h> #include <string.h> #include <stdlib.h> char caesar (char x, char alphabets[]); int j; int main() { char*

我为凯撒的密码写了一个密码,这个密码有效,除了我不能加密超过8个字母,我也不能处理空格。它显示“>>”此符号,而不是空格。另外,我想在代码的第二个函数中进行二进制搜索,但我不知道我是否已经这样做了

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

char caesar (char x, char alphabets[]);

int j;

int main()

{

    char* plain_text = malloc (10 * sizeof(char)  + 1);
    int key, num;
    char* cipher_text = malloc (10 * sizeof(char)  + 1);

    printf("Plain text: ");
    gets(plain_text);
    printf("\nThe plain text is:  ");
    puts(plain_text);
    printf("\nKey: ");
    scanf("%d", &key);
    num = (int)key;

    if (key != num)
    {
        return 1;
    }
    int i;
    char alphabets[] = {'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'};
    for (i=0;i<=strlen(plain_text);i++)
    {
        char x = plain_text[i];
        caesar(x, alphabets);
        cipher_text[i] = alphabets[j+key];
        printf("%c", cipher_text[i]);
    }
    free(plain_text);
}

char caesar (char x, char alphabets[])

{

    if(x == alphabets[13])
    {
        return 13;
    }
    for(j = 1; j <= 13; j++)
    {
        if(x == alphabets[j])
        {
            return j;
        }
    }
    for(j = 13; j <= strlen (alphabets); j++)
    {
        if(x == alphabets[j])
        {
            return j;
        }
    }
}
#包括
#包括
#包括
char caesar(char x,char alphabets[]);
int j;
int main()
{
char*plain_text=malloc(10*sizeof(char)+1);
int键,num;
char*cipher_text=malloc(10*sizeof(char)+1);
printf(“纯文本:”);
获取(纯文本);
printf(“\n纯文本为:”);
puts(纯文本);
printf(“\nKey:”);
scanf(“%d”和键)(&key);
num=(int)键;
如果(键!=num)
{
返回1;
}
int i;
字符字母[]={'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';
for(i=0;i
caesar()
似乎只是以一种过于复杂的方式返回字符b到z在数组中的位置,并完全省略了a!而且,由于
字母表
不是以空结尾的字符串,因此
strlen()
在任何情况下都不是有效的操作。“加密”已完成(错误地)通过
main()
中的
alphabets[j+key]
使
caesar()
的名字特别难听——因为它根本不是这样做的

以下函数将返回
字母表
中任何字符的密码,而不修改任何其他字符:

char caesar( char x, int key )
{
    const char alphabet[] = {'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 cipher = x ;

    for( int i = 0; 
         cipher == x && i < sizeof( alphabet ); 
         i++ )
    {
        if( alphabet[i] == x )
        {
            cipher = alphabet[(i + key) % sizeof( alphabet )] ;
        }
    }

    return cipher ;
}

从不使用
get
函数。这是因为它已从C中完全删除。请使用例如。以及为什么在赋值
num=(int)中强制转换key
?这两个
num
key
都是
int
类型。赋值之后,
key!=num
将始终为false。您真的要对空终止符进行加密吗?听起来不正确。最后,您对函数
caesar
返回的值做了什么?一个d如果最后一个循环结束时您没有返回值,会发生什么情况?
strlen(alphabets)
是不确定的,因为
alphabets
不是以nul结尾的字符串。
const char*alphabets=“abcdefghijjklmnopqrstuvwxyz”
会更有意义。先生,您能解释一下函数(char-caesar(char-x,int-key)是什么吗是吗?我是一个新手,很难理解。@riki:我已经简化了它并添加了一个解释-希望有帮助。@riki:进一步简化(对于循环,而不是使用时)并添加了支持大写的解决方案。@riki:添加了算术解决方案(并解释)…先生,您能看到这个问题吗?
for (i = 0; i <= strlen(plain_text); i++ )
{
    cipher_text[i] = caesar( plain_text[i], key ) ;
}
#include <stdio.h>
#include <string.h>

char caesar( char x, int key ) ;

#define MAX_TEXT 128

int main()
{
    char plain_text[MAX_TEXT] = "" ;
    char cipher_text[MAX_TEXT] = "" ;

    printf( "Plain text: " );
    fgets( plain_text, MAX_TEXT, stdin ) ;

    printf( "\nThe plain text is: %s\n", plain_text ) ;

    printf( "Key: " ) ;
    int key = 0 ;
    scanf( "%d", &key );

    for( size_t i = 0; i <= strlen( plain_text ); i++ )
    {
        cipher_text[i] = caesar( plain_text[i], key ) ;
    }

    printf( "\nThe cipher text is: %s\n", cipher_text ) ;
    return 0 ;
}
Plain text: abc, xyz

The plain text is: abc, xyz

Key: 1

The cipher text is: bcd, yza
#include <ctype.h>

char caesar( char x, int key )
{
    const char alphabet[] = {'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 cipher = x  ;

    for( int i = 0;
         cipher == x && i < sizeof( alphabet );
         i++ )
    {
        if( alphabet[i] == tolower( x ) )
        {
            cipher = alphabet[(i + key) % sizeof( alphabet )] ;
            if( isupper( x ) )
            {
                cipher = toupper( cipher ) ;
            }
        }
    }

    return cipher ;
}
Plain text: aBc, XyZ 123

The plain text is: aBc, XyZ 123

Key: 1

The cipher text is: bCd, YzA 123
char caesar( char x, int key )
{
    char cipher = tolower( x ) ;

    if( isalpha( x ) )
    {
        cipher = ((cipher - 'a') + key) % ('z' - 'a' + 1) + 'a' ;
        if( isupper( x ) )
        {
            cipher = toupper( cipher ) ;
        }
    }

    return cipher ;
}