Welcome Guest.

1Answers

c language operation problem

Asked by: Frank Hill 25 views IT July 26, 2018

#include<stdio.h>

#include<string.h>

 

void jiami(char c[],int a[]){ 

 

    for(int i = 0, j = 0; c[j];j++, i = (i + 1) % 8){

 

        c[j]+=a[i];

 

        if(c[j] > 122) {

c[j] -= 90;}

    }

}

void jiemi(char c[],int a[]){ 

 

    for(int i = 0, j = 0; c[j];j++, i = (i + 1) % 8){

 

        c[j]-=a[i];

 

        if(c[j] < 32) 

{

c[j] += 90;}

    }

}

void xiangdeng(char c[],char b[])

{

c=b;

}

 

int main()

{

 

    int a[] = {5,7,8,6,4,3,9,2};

 

    char s[50],s1[50];

 

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

    scanf("%s",&s);

    xiangdeng(s1,s); 

 

    jiami(s,a);

 

    printf("加密后密文:%s",s);

 

    jiemi(s,a);

 

    if(s1==s) 

printf("解密后密文:%s",s); 

 

    else 

printf("解密错误!"); 

 

}

为什么总出现解密错误

1 Answers

  1. +4Votes  

    I didn’t see your request. There is a problem with the algorithm

    122 is the ASCII of ‘z’ and 90 is the ‘A’ ASCII code, while in the middle of A~z, the ASCII is not continuous for the letters

    So if you include capitalization, you should consider this issue

    In addition, your program has two basic errors

    void xiangdeng(char c[],char b[] )

    {

    c=b;

    }

    will not change anything (c, b are arguments, in The function does not change)

    C language string assignment, can only use strcpy function

    and the same

    if(s1==s)  

    This is a comparison of two pointers, they can never be equal.

    C string comparison can not be used ==, to use the function strcmp

    Phillips- July 27, 2018 |