用c语言实现凯撒密码(c语言凯撒加密字符串的代码)

2023-03-02 12:20:57 摩斯密码知识 思思

凯撒密码,C语言,求救!

#include stdio.h

#include string.h

int main()

{

int i = 0;

int len = 0;

char ch;

char buf[256] = {0};

char nor[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 enc[26] = {'s','u','w','y','a','c','e','g','i','k','m','o','q','r','t','v','x','z','b','d','f','h','j','l','n','p'};

printf("Encode or Decode: ");

scanf("%c",ch);

printf("please input your string: ");

fflush(stdin);

gets(buf);

len = strlen(buf);

switch (ch)

{

case 'e':

case 'E':

for (i=0;ilen;i++)

{

buf[i] = enc[buf[i] - 'a'];

}

break;

case 'd':

case 'D':

for (i=0;ilen;i++)

{

buf[i] = nor[i];

}

break;

default:

printf("wrong input!\n");

}

printf("%s\n",buf);

return 0;

}

凯撒密码 C语言

#includestdio.h

#includestring.h

void main ()

{

char str[100];

char str1[100];

printf("输入字符串:");

scanf("%s",str);

int len;

len=strlen(str);

for(int i=0;i<len;i++)

{

str1[i]=(str[i]-97+3)%26+97;

}

str1[len]='\0';

printf ("密文为:%s\n",str1);

}

用c语言实现凯撒密码(c语言凯撒加密字符串的代码) 第1张

用C语言编程恺撒密码加密解密程序

#include stdio.h

#define isletter( c )    ( ((c)='a'(c)='z') || ((c)='A'(c)='Z') )

void Enc( const char *str, char *out, int key )

{

    int i = 0; 

    while( str[i] )

    {

        if ( isletter( str[i] ) )

        {

            out[i] = str[i] + key;

            if ( ! isletter( out[i])  )

                out[i] -= 26;

        }

        else

            out[i] = str[i];

        i++;

    }

    out[i] = 0;

}

void Denc( const char *str, char *out, int key )

{

    int i=0;

    while( str[i] )

    {

        if ( isletter( str[i] ) )

        {

            out[i] = str[i] - key;

            if ( ! isletter( out[i] ) )

                out[i] += 26;

        }

        else

            out[i] = str[i];

        i++;

    }

    out[i] = 0;

}

int main()

{

    char  out[100], out2[100];

    Enc( "THE QUICK BROWn fox jumps over THE LAZY DOG", out, 3 );

    printf( "%s\n", out );

    Denc( out, out2, 3 );

    printf( "%s\n", out2 );

}

凯撒密码,要求C语言编写,求救!

写的一般般,希望对LZ有所帮助

#include stdio.h

#include string.h

int main()

{

char str[201];//存放字符

char tmp[11];//临时变量

int i;//循环变量

int len;//存放消息长度

scanf("%s",tmp);//这里输入START,开始

getchar();//接收回车

while(strcmp(tmp,"ENDOFINPUT"))

{

gets(str);//由于输入中有空格,所以用gets输入

getchar();//接收回车

len = strlen(str);

for(i=0;ilen;i++)

{

if(str[i]='A'str[i]='Z')

{

str[i] -= 5 ;

if(str[i] 65)

{

str[i] +=26;

}

}

}

scanf("%s",tmp);//这里输入END,结束

printf("%s\n",str);//处理完就直接输出结果

scanf("%s",tmp);//输入START表示继续,输入ENDOFINPUT则表示最后一个数据集

getchar();//接收回车

}

return 0;

}