zxd3014779200

凯撒密码密码的c语言代码(进阶版)
#include <stdio.h> #include <string.h> #inclu...
扫描右侧二维码阅读全文
12
2019/12

凯撒密码密码的c语言代码(进阶版)

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include<stdlib.h>
int g( char*);
int main()
{
    char b[25] = { 0 };
    int n,i;
    int m = 0;
    int o = 2;
    printf("请输入移位数的值:");
    scanf_s("%d", &n);//如果报错 请用scanf函数
    getchar();    //去掉空格
    while (o == 2)
    {
        printf("请输入明文:");
        gets_s(b);
        //getchar();    //去掉空格
        int s;
        s = g(b);
        for (i = 0;i<s; i++)    //获取密钥的长度
        {
            //把大写字母转化为小写字母
            if (b[i] >= 'A' && b[i] <= 'Z')
            {
                b[i] += 32;
            }

            if (b[i] >= 'a' && b[i] <= 'z')
            {
                b[i] = ((b[i] - 'a') + n) % 26 + 'a';
                o = 3;
            }
            if (b[i] < 'A' || b[i] > 'z')
            {
                printf("该程序仅支持大小写字母\n");
                printf("请重新输入\n");
                Sleep(1000);//延迟1s
                system("cls");//清屏
                //错误字符无论在什么位置都可以识别
                o = 2;
                break;
            }
        }
        if(o==3)
        {
            printf("密钥:");
            for (i = 0; i <s; i++)
            {
                printf("%c", b[i]);
            }
        }
    }
    
    getchar();
    return 0;
}

int g( char *str)
{    
    int count = 0;
    while (*str != '\0')
    {
        count++;
        str++;
    }
    return count;
}
Last modification:December 12th, 2019 at 10:17 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment