C语言实现字符串移位加密

本程序使用循环遍历字符串,将每个字母往后移3位,x、y、z分别变成a、b、c,其他字母不变,最终输出加密后的字符串。

#include <stdio.h>

int main() {
    char s[100];
    char encrypted[100] = {0};
    int i = 0;

    printf("请输入字符串:");
    gets(s);

    while (s[i] != '\0') {
        if (isalpha(s[i])) {
            if (s[i] == 'x') {
                encrypted[i] = 'a';
            } else if (s[i] == 'y') {
                encrypted[i] = 'b';
            } else if (s[i] == 'z') {
                encrypted[i] = 'c';
            } else {
                encrypted[i] = (s[i] + 3 > 'z') ? (s[i] - 23) : (s[i] + 3);
            }
        } else {
            encrypted[i] = s[i];
        }
        i++;
    }

    printf("加密后的字符串:%s\n", encrypted);
    return 0;
}

示例:

输入:hello world!

输出:khoor zruog!


原文地址: https://www.cveoy.top/t/topic/oJU0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录