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

用C语言编程恺撒密码加密解密程序,第1张

#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 )

}

#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=0i<leni++)

{

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

}

break

case 'd':

case 'D':

for (i=0i<leni++)

{

buf[i] = nor[i]

}

break

default:

printf("wrong input!\n")

}

printf("<%s>\n",buf)

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7804362.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存