#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;ilen;i++)
{
buf[i] = enc[buf[i] - 'a'];
}
break;
case 'd':
case 'D':
for (i=0;ilen;i++)
{
buf[i] = nor[i];
}
break;
default:
printf("wrong input!\n");
}
printf("%s\n",buf);
return 0;
}
/*
和楼上的相比,或许 看上去很烦
ch[i] +=5;
if (ch[i] 'Z')
{
ch[i] -= 26;
}
可以改成和 楼上的 方法
等价于 ch[i] = 'A' + (ch[i] - 'A' + 5) % 26;
*/
# include stdio.h
# include stdlib.h //用到了system(); 不写 ,可以用 getchar();
#define strwidth 117 //定义长度
int main(void)
{
char ch[strwidth];
int i ;
printf("请输入密码:");
gets(ch); //输入数据,用gets(); 保留了空格
for (i = 0; i strwidth ;i++ )
{
if (ch[i] = 'a' ch[i] = 'z' ) //判断是否小写字母
{
ch[i] +=5;
if (ch[i] 'z') //不解释,我想这样,理解可能会方便点吧
{
ch[i] -= 26;
}
}
else if ( ch[i] = 'A' ch[i] = 'Z') //判断是否大写字母
{
ch[i] +=5;
if (ch[i] 'Z')
{
ch[i] -= 26;
}
}
}
printf("加密后为:%s\n" , ch); //输出数据
system("pause");
return 0;
}
/*
或者 这样
*/
# include stdio.h
# include stdlib.h //用到了system(); 不写 ,可以用 getchar();
#define strwidth 117 //定义长度
int main(void)
{
char ch[strwidth];
int i ;
printf("请输入密码:");
gets(ch); //输入数据,用gets(); 保留了空格
for (i = 0; i strwidth ;i++ )
{
if (ch[i] = 'a' ch[i] = 'z' || ch[i] = 'A' ch[i] = 'Z' ) //判断是否是字母
{
ch[i] +=5;
if ( ch[i]'Z' ch[i] = 'Z' + 5 || ch[i] 'z' )
{
ch[i] -= 26;
}
}
}
printf("加密后为:%s\n" , ch); //输出数据
system("pause");
return 0;
}
#include stdio.h
#define isletter( c ) ( ((c)='a'(c)='z') || ((c)='A'(c)='Z') )
void Enc( const char *str, char *out, int key )
{
int i = 0;
while( str[i] )
{
if ( isletter( str[i] ) )
{
out[i] = str[i] + key;
if ( ! isletter( out[i]) )
out[i] -= 26;
}
else
out[i] = str[i];
i++;
}
out[i] = 0;
}
void Denc( const char *str, char *out, int key )
{
int i=0;
while( str[i] )
{
if ( isletter( str[i] ) )
{
out[i] = str[i] - key;
if ( ! isletter( out[i] ) )
out[i] += 26;
}
else
out[i] = str[i];
i++;
}
out[i] = 0;
}
int main()
{
char out[100], out2[100];
Enc( "THE QUICK BROWn fox jumps over THE LAZY DOG", out, 3 );
printf( "%s\n", out );
Denc( out, out2, 3 );
printf( "%s\n", out2 );
}
#includestdio.h
#includestring.h
int main()
{
int i;int number;
char a[100];
scanf("%s",a);
number=strlen(a);
for(i=0;inumber;i++){
a[i]=a[i]+4;
}
for(i=0;inumber;i++){
printf("%c",a[i]);
}
return 0;
}
我尽量用注释阐述了思路,希望可以帮到你!!
#includestdio.h
#includestring.h
#define N 80 //可加密字符串最大长度
char plaintext[N]={0}; //明文,输入时输入字符,参与运算时强制转换成整数
int ciphertext[N]={0}; //密文,保存成整数,输出时强制转换成字符
int k; //后(右)移位数,相当于密钥
void getPlainText() //获得明文字符串
{
printf("请输入明文:");
scanf("%s",plaintext);
printf("\n");
}
void getLength() //获取后(右)移位数(密钥)
{
printf("请输入后移的位数:");
scanf("%d",k);
k%=26; //因为字母只有26个,所以超过26相当于重复
}
void Caesar_cipher() //凯撒加密,本程序采用的是字母循环后(右)移
{
unsigned int i;
for(i=0;istrlen(plaintext);i++)
{
//两个bool类型的变量是为了判断字符是否是字母(包括大写和小写)
bool flag1=plaintext[i]='a'plaintext[i]='z';
bool flag2=plaintext[i]='A'plaintext[i]='Z';
if(flag1||flag2){ //如果是字母,加密
ciphertext[i]=(int)plaintext[i]+k; //字母在字母表中后(右)移K位
if(ciphertext[i](int)'z'){ //保证是循环后(右)移
ciphertext[i]-=26;
}
}
else //非字母字符,不做处理,原样保存
ciphertext[i]=(int)plaintext[i];
}
}
void printCipherText() //输出加密后的密文
{
unsigned int i;
printf("\n加密后的密文是:");
for(i=0;istrlen(plaintext);i++) //把参与计算后是整数强制转换成对应的字符
printf("%c",(char)ciphertext[i]);
printf("\n");
}
void main()
{
getPlainText(); //明文
getLength(); //后(右)移位数
Caesar_cipher(); //凯撒加密
printCipherText(); //密文
}
能不能说清楚一点,是加密吗?
#include stdio.h
#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", 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;