C语言编程问题,凯撒密码
从指定的文件中读出原文,使用凯撒加密算法加密,将原文和密文分别输出到屏幕上,移位数位k,k从键盘输入。(指定文件在D盘)测试输入3则输出明文:↵Beijing...
从指定的文件中读出原文,使用凯撒加密算法加密,将原文和密文分别输出到屏幕上,移位数位k,k从键盘输入。(指定文件在D盘)
测试输入3
则输出
明文:↵
Beijing Technology and Business University (BTBU) is a key state-run university with comprehensive disciplines covering Arts, Sciences, Engineering, Law, Economics, History, Philosophy and Management.↵
密文:↵
Ehlmlqj Whfkqrorjb dqg Exvlqhvv Xqlyhuvlwb (EWEX) lv d nhb vwdwh-uxq xqlyhuvlwb zlwk frpsuhkhqvlyh glvflsolqhv fryhulqj Duwv, Vflhqfhv, Hqjlqhhulqj, Odz, Hfrqrplfv, Klvwrub, Sklorvrskb dqg Pdqdjhphqw.↵ 展开
测试输入3
则输出
明文:↵
Beijing Technology and Business University (BTBU) is a key state-run university with comprehensive disciplines covering Arts, Sciences, Engineering, Law, Economics, History, Philosophy and Management.↵
密文:↵
Ehlmlqj Whfkqrorjb dqg Exvlqhvv Xqlyhuvlwb (EWEX) lv d nhb vwdwh-uxq xqlyhuvlwb zlwk frpsuhkhqvlyh glvflsolqhv fryhulqj Duwv, Vflhqfhv, Hqjlqhhulqj, Odz, Hfrqrplfv, Klvwrub, Sklorvrskb dqg Pdqdjhphqw.↵ 展开
3个回答
展开全部
#include <stdio.h>
char s[]="Beijing Technology and Business University (BTBU) is a key state-run university with comprehensive disciplines covering Arts, Sciences, Engineering, Law, Economics, History, Philosophy and Management.";
void fun(char *s,int n)
{
int i=0;
while(s[i]!=0)
{
if(s[i]<='z'&&s[i]>='a')
{
s[i] += n;
if(s[i]>'z') s[i] -=24;
}
else if(s[i]<='Z'&&s[i]>='A')
{
s[i] += n;
if(s[i]>'Z') s[i] -=24;
}
i++;
}
}
void main()
{
fun(s,3);
puts(s);
}
展开全部
public static void encrypt(String str, int k) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char) (ch + k % 26);
if (ch > 'Z') {
ch = (char) (ch - 26);
}
}
if (ch >= 'a' && ch <= 'z') {
ch = (char) (ch + k % 26);
if (ch > 'z') {
ch = (char) (ch - 26);
}
}
System.out.print(ch);
}
}
输入字符串和位移位数,直接输出编码加密后的字符串,或者也可以自己改成将加密后的字符串返回。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<ctype.h>
int main()
{
int i,k=0;
char s[1000];
scanf("%d%*c", &k);
gets(s);
for( i=0;s[i]!='\0';i++ )
{
if ( s[i]>='a' && s[i]<='z' )
{
s[i] += k ;
if ( s[i] > 'z' )
s[i]='a'+(s[i]-'z'-1) ;
}
else if ( s[i]>='A' && s[i]<='Z' )
{
s[i] += k ;
if ( s[i] > 'Z' )
s[i]='A'+(s[i]-'Z'-1) ;
}
printf("%c", s[i] );
}
printf("\n");
return 0;
}
追问
我是要从指定文件中读出来,指定文件在d盘
追答
FILE *fp;
fp=fopen("d:/text.txt", "r" );
if ( fp )
{
while( fgets(s,sizeof(s),fp) )
{
for( i=0;s[i]!='\0';i++ )
{
if ( s[i]>='a' && s[i]<='z' )
{
s[i] += k ;
if ( s[i] > 'z' )
s[i]='a'+(s[i]-'z'-1) ;
}
else if ( s[i]>='A' && s[i]<='Z' )
{
s[i] += k ;
if ( s[i] > 'Z' )
s[i]='A'+(s[i]-'Z'-1) ;
}
printf("%c", s[i] );
}
}
fclose(fp);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询