凯撒密码C语言用ASCII码,C凯撒密码ASCII字母换行

我对C很新颖。我希望能够移动字母'x'的次数来创建基本密码。C凯撒密码ASCII字母换行

我遇到了islower()函数的问题。我使用'我',但是,我无法将其更改为角色。

#include

#include

#include

#include

#include

string p;

int main(int argc, string argv[])

{

//if argument count does not equal 2, exit and return 1

if (argc != 2)

{

printf("Less or more than 2 arguments given, exiting...\n");

return 1;

}

else //prompt user for plaintext to encrypt

{

p = GetString();

}

//take the second part of the array (the int entered by user) and store as k (used as the encryption key)

//string k = argv[1];

int k = atoi(argv[1]);

//function:

// c = (p + k) % 26;

//iterate over the characters in the string

//p represents the position in the alphabet of a plaintext letter

//c likewise represents a position in the alphabet

char new;

for (int i = 0, n = strlen(p); i < n; i++)

if (islower((char)i))

{

//printf("%c\n", p[i] + (k % 26));

printf("This prints p:%s\n", p);

printf("This prints i:%d\n", (char)i);

printf("This prints k:%d\n", k);

printf("This prints output of lower(i):%d\n", islower(i));

new = (p[i] - 97);

new += k;

//printf("%d\n", new %26 + 97);

//printf("i = |%c| is lowercase\n", i);

printf("%c\n", new % 26 + 97);

}

else {

//printf("%c", p[i] + (k % 26));

printf("This prints p:%s\n", p);

printf("This prints i:%d\n", (char)i);

printf("This prints k:%d\n", k);

printf("This prints output of lower(i):%d\n", islower(i));

new = (p[i] - 65);

new += k;

//printf("%d\n", new % 26 + 65);

//printf("i = |%c| is uppercase\n", i);

printf("%c\n", new % 26 + 65);

}

printf("\n");

}

输出:

[email protected] (~/Dropbox/CS50x/pset2): ./caesar2 1

zZ < here is my input

This prints p:zZ

This prints i:0

This prints k:1

This prints output of lower(i):0

G < here is fails, lower case z should move to lower case a

This prints p:zZ

This prints i:1

This prints k:1

This prints output of lower(i):0

A < here is a success! upper case Z moves to upper case A

2014-03-05

JT1

+1

模运算符'%'具有比'+'更高的优先级。如果我是你,我会在'printf()'中使用圆括号。 –

+0

我已经更新了,谢谢。 –

+0

我认为reza的意思是(p [i] + k)%26。 –

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值