凯撒密码,C语言,求救!

来自:    更新日期:早些时候
凯撒密码,要求C语言编写,求救!~

写的一般般,希望对LZ有所帮助
#include
#include
int main()
{
char str[201];//存放字符
char tmp[11];//临时变量
int i;//循环变量
int len;//存放消息长度
scanf("%s",tmp);//这里输入START,开始
getchar();//接收回车
while(strcmp(tmp,"ENDOFINPUT"))
{
gets(str);//由于输入中有空格,所以用gets输入
getchar();//接收回车
len = strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i] -= 5 ;
if(str[i] < 65)
{
str[i] +=26;
}
}
}
scanf("%s",tmp);//这里输入END,结束
printf("%s
",str);//处理完就直接输出结果
scanf("%s",tmp);//输入START表示继续,输入ENDOFINPUT则表示最后一个数据集
getchar();//接收回车
}
return 0;

}

能不能说清楚一点,是加密吗?
#include
#include
#define MAXSIZE 81

int main()
{
char str[MAXSIZE];
int i;
int offset;
int n;

printf("请输入要加密的字符串:"); //最大输入个数是80个字符
gets(str);
printf("请输入要偏移量:"); //若将a变为b,则偏移量为1,以此类推,偏移量在1-25之间
scanf("%d%*c", &offset);
n = strlen(str);
for (i = 0; i < n; i++)
{
if ('a' <= str[i] && str[i] <= 'z' - offset || 'A' <= str[i] && str[i] <= 'Z' - offset)
str[i] += offset;/*判断字符是否是字母,以及加密后是还是字母*/
else/*超出字母范围时,减26*/
str[i] += offset - 26;
}
printf("加密后的字符串是:");
puts(str);
return 0;

#include <stdio.h>
#include <string.h>

int main()
{
int i = 0;
int len = 0;
char ch;
char buf[256] = {0};
char nor[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char enc[26] = {'s','u','w','y','a','c','e','g','i','k','m','o','q','r','t','v','x','z','b','d','f','h','j','l','n','p'};

printf("Encode or Decode: ");
scanf("%c",&ch);
printf("please input your string: ");
fflush(stdin);
gets(buf);
len = strlen(buf);

switch (ch)
{
case 'e':
case 'E':
for (i=0;i<len;i++)
{
buf[i] = enc[buf[i] - 'a'];
}
break;
case 'd':
case 'D':
for (i=0;i<len;i++)
{
buf[i] = nor[i];
}
break;
default:
printf("wrong input!\n");
}

printf("<%s>\n",buf);

return 0;
}

定义一下表里对应字符的差值,加密-差值,解密+差值
#include <stdio.h>
#include <string.h>
void encode(char s[]);

void decode(char s[]);

char plain[] = "abcdefghijklmnopqrstuvwxyz";
char cipher[] = "suwyacegikmoqrtvxzbdfhjlnp";
char plain2cipher[26];
char cipher2plain[26];
int main(){

char s[256];
int i;
for(i = 0; plain[i] ;i++){
plain2cipher[plain[i]-'a'] = cipher[i] - plain[i];
cipher2plain[cipher[i]-'a'] = plain[i] - cipher[i];
}
printf("Input a string:");
scanf("%s",s);
encode(s);

puts(s);

decode(s);

puts(s);

}

void encode(char s[])
{
int i=0;
while(s[i]){
s[i] += plain2cipher[s[i]-'a'];
i++;
}
}

void decode(char s[]){
int i=0;
while(s[i]){
s[i] += cipher2plain[s[i]-'a'];
i++;
}
}

我可以问一下算法策略和算法数据结构设计是什么吗?
算法策略 数组映射

数据结构 数组


凯撒密码,C语言,求救!视频

相关评论:
  • 13583954430凯撒密码,要求C语言编写,求救!
    柴曼怨写的一般般,希望对LZ有所帮助 include <stdio.h> include <string.h> int main(){ char str[201];\/\/存放字符 char tmp[11];\/\/临时变量 int i;\/\/循环变量 int len;\/\/存放消息长度 scanf("%s",tmp);\/\/这里输入START,开始 getchar();\/\/接收回车 while(strcmp(tmp,"ENDOFINPUT")){ g...

  • 13583954430凯撒密码怎么用C语言编,急救!!!附加解释,谢谢
    柴曼怨凯撒加密是最简单的加密,就是 把字符移动n位, 例如 :移动1位时,a就用b表示,f用e表示。include<stdio.h> int main(void){ char buf[] = "hello";int i = 0;printf("before: %s\\n", buf);while (buf[i])buf[i++] += 1; \/\/ 移1位,a 变b printf("after: %s\\n", buf...

  • 13583954430凯撒密码用C语言编写,求救!!!
    柴曼怨include <string.h> define MAXSIZE 81 int main(){ char str[MAXSIZE];int i;int offset;int n;printf("请输入要加密的字符串:"); \/\/最大输入个数是80个字符 gets(str);printf("请输入要偏移量:"); \/\/若将a变为b,则偏移量为1,以此类推,偏移量在1-25之间 scanf("%d%*c", ...

  • 13583954430用C语言实现凯撒密码加密解密,急!
    柴曼怨凯撒密码就是简单的加上一个数,'a'+3='d';'z'+3='c' 假设原文全是小写字母,那么 char plain[N]={...}; \/\/明文 char cipher[N]={};\/\/密文 int key=3; int i=0,temp; for(i=0;i<N;i++) {if(plain[i]!=' ') {temp=plain[i]+key-'a'; temp=temp%26; cipher[i...

  • 13583954430凯撒密码,C语言,求救!
    柴曼怨include <string.h> int main(){ int i = 0;int len = 0;char ch;char buf[256] = {0};char nor[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};char enc[26...

  • 13583954430C语言编程问题,凯撒密码
    柴曼怨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]>'...

  • 13583954430凯撒密码(Caesar)的原理和算法实现(C语言)
    柴曼怨在C语言中,我们可以通过以下代码实现凯撒密码的加密和解密功能,代码逻辑清晰,处理了大小写字母的移位操作:```cppint kaisa_encrypt(char* text, char* result, int k) { for (int i = 0; text[i] != '\\0'; i++) { if (text[i] >= 'a' && text[i] <= 'z') { i...

  • 13583954430C语言写凯撒加密
    柴曼怨修改后如下 供参考 include <stdio.h>void main(){char alphabet[81] = "abcdefghijklmnopqrstuvwxyz";int num1, num2;void encrypt(char [], int);void deciphering(char [], int);printf("Please type number to choose a model(encrypt 1\/deciphering 2):");scanf("%d", &num1);get...

  • 13583954430将凯撒密码X的加密、解密过程用C语言编程实现
    柴曼怨1、在密码学中,恺撒密码(或称恺撒加密、恺撒变换、变换加密)是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密...

  • 13583954430凯撒密码的算法c语言的怎么实现啊?
    柴曼怨下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。include <stdio.h> main(){ char M[100];char C[100];int K=3,i;printf("请输入明文M(注意不要...

  • 相关主题精彩

    版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

    Copyright © 喜物网