C语言密码加密问题,求解答

img


这个用C语言怎么写啊,希望大家能够帮忙,急需一个答案,谢谢大家了!

将整数每位数字取出,对应位置的字符加上数字后求余26,公式为(c-'A'+k)%26

#include <stdio.h>
int main()
{
    int n,m,c,t=10000;
    char s[6];
    while(scanf("%d%s",&n,s) != EOF)
    {
        t = 10000;
        for(int i=4;i>=0;i--)
        {
            m = n/t;
            n%=t;
            t/=10;
            c = (s[4-i]-'A'+m)%26 + 'A';
            putchar(c);
        }
        printf("\n");
 }
}


#include <stdio.h>
int main()
{
    int n,y;
    char str[6];
    while (scanf("%d%s", &n, str) != EOF)
    {
        for (int i = 4; i>=0; i--)
        {
            y = n % 10;
            n/= 10;
            str[i] = str[i]+y;
        }
        printf("%s\n", str);
    }
    return 0;
}

可以试试这个(可多组输入输出):

#include <stdio.h>
int main()
{
    int num;
    char s[6];
    while(scanf("%d %s",&num,s) != EOF){
        int w = (num/10000)%10;    //万位数字
        int q = (num/1000)%10;    //千位数字
        int b = (num/100)%10;    //百位数字
        int s1 = (num/10)%10;        //十位数字
        int g = (num/1)%10;        //个位数字
        printf("%c%c%c%c%c\n",(int)s[0]+w,(int)s[1]+q,(int)s[2]+b,(int)s[3]+s1,(int)s[4]+g);
        w=0;q=0;b=0;s1=0;g=0;
    }
    
    return 0;
}

测试图:

img

如有帮助,还请采纳!谢谢!

两种方法:第二种方法更方便一些。
(1)n作为正数读入

#include<stdio.h>
int main()
{
    int n, i, t;
    char s[6] = { 0 }, c;
    while (scanf("%d %s", &n, s) != EOF)
    {
        t = 10000;
        i = 0;
        while (i<5) 
        {
            c = s[i] + n / t;
            printf("%c", c);
            i++;
            n %= t;
            t /= 10;
        }
    }
    return 0;
}

(2)n作为字符读入

#include<stdio.h>
int main()
{
    char n[6] = { 0 };
    char s[6] = { 0 }, c;
    int i;
    while (scanf("%s %s", n, s) != EOF)
    {
        i = 0;
        while (i<5) 
        {
            c = s[i] + n[i]-'0';
            printf("%c", c);
            i++;
        }
    }
    return 0;
}