C语言密码移位问题?

Python010

C语言密码移位问题?,第1张

明文移动k位之后,如果超出了字母z,如:z字母向右移动16位,已经超出了范围,就需要经过处理

char e(char m,int k)

{

    m=m+k

    if(m>90) m=k-90+65//这里涉及到按键码问题,每个键盘都有一个asc2码,Z=90,A=65

    return m

}

#include<stdio.h>

#include<string.h>

void main ()

{

char str[100]

char str1[100]

printf("输入字符串:")

scanf("%s",&str)

int len

len=strlen(str)

for(int i=0i<leni++)

{

str1[i]=(str[i]-97+3)%26+97

}

str1[len]='\0'

printf ("密文为:%s\n",str1)

}

这道题,并不难,只是楼主,没有说清,是就字母移位吗?

但是看你的例子,有不全是。

程序如下:

#include <stdio.h>

#include <stdlib.h>

FILE *source//源文件

FILE *destination//目标文件

int key//密钥

char file[100]//文件名

void encryption()//加密

{

char ch

printf("请输入要加密的文件名\n")

scanf("%s",file)

if((source=fopen(file,"r"))==NULL)

{

printf("无法打开文件!\n")

exit(0)

}

printf("请输入加密后的文件名\n")

scanf("%s",file)

if((destination=fopen(file,"w"))==NULL)

{

printf("无法创建文件!\n")

exit(0)

}

printf("请输入密钥\n")

scanf("%d",&key)

ch=fgetc(source)

while(ch!=EOF)

{

if(ch=='\n')

{

fputc(ch,destination)

ch=fgetc(source)

continue

}

ch+=key

fputc(ch,destination)

ch=fgetc(source)

}

fclose(source)

fclose(destination)

}

void decrypt()//解密

{

char ch

printf("请输入要解密的文件名\n")

scanf("%s",file)

if((source=fopen(file,"r"))==NULL)

{

printf("无法打开文件!\n")

exit(0)

}

printf("请输入加密后的文件名\n")

scanf("%s",file)

if((destination=fopen(file,"w"))==NULL)

{

printf("无法创建文件!\n")

exit(0)

}

printf("请输入密钥\n")

scanf("%d",&key)

ch=fgetc(source)

while(ch!=EOF)

{

if(ch=='\n')

{

fputc(ch,destination)

ch=fgetc(source)

continue

}

ch-=key

fputc(ch,destination)

ch=fgetc(source)

}

fclose(source)

fclose(destination)

}

int main()//主函数提供菜单

{

int choice=0

printf("******************\n")

printf("1 文件加密\n")

printf("2 文件解密\n")

printf("3 退出\n")

printf("******************\n")

printf("请输入1 2 3选择操作\n")

scanf("%d",&choice)

switch(choice)

{

case 1:encryption()break

case 2:decrypt()break

case 3:break

}

return 0

}