永发信息网

C语言 循环加密求助

答案:5  悬赏:70  手机版
解决时间 2021-02-09 15:20
  • 提问者网友:几叶到寒
  • 2021-02-08 17:57
输入一个KEY,和一个字符串, 按照KEY对字符传替换. 例如
KEY: 4
MESSAGE: ABCDE
结果为: EFGHI
同样适合于长字符串 如输入 i am a boy 步长为4的话
结果为:m eq m ...

如果KEY为-1
MESSAGE:IBM
结果为向前移动:HAL

只有字符传才移动,如果是数字什么的就保留不动.
tanyugou 题目没有理解正确,
hh9090, 你的code中当key = 负值时,加密后的字母"a"显示为符号,望修改
最佳答案
  • 五星知识达人网友:过活
  • 2021-02-08 19:21


#include

int Encrypt(char*, int);

int main()
{
char sSrc[256] = { 0 };
int iKey = 0;
printf("Please input a string to be encrypted:\n");
scanf("%s", sSrc);
printf("Please input a number for encryption:\n");
scanf("%d", &iKey);
if ( Encrypt( sSrc, iKey ))
printf("Cryptograph: %s\n", sSrc);
else
printf("No Source String for Encryption.\n");
getch();
return 0;
}

int Encrypt(char* sEn, int iKey)
{
int i = 0;
int ilen = strlen(sEn);
if ( !ilen )
return 0;
for (i=0; i {
int t = *(sEn+i);
if (t>=65 && t<=90)
{
t = (t - 65 + iKey%26)%26;
t = (t>=0?t:(t+26)) + 65;
}
else if (t>=97 && t<=122)
{
t = (t - 97 + iKey%26)%26;
t = (t>=0?t:(t+26)) + 97;
}
else
continue;

*(sEn+i) = t;
}
return 1;
}
全部回答
  • 1楼网友:猎心人
  • 2021-02-08 22:06
#include main() { char str[]="IBM is a famous company."; int i,key=-1; for(i=0;str[i];++i) if(str[i]>='A' && str[i]<='Z') str[i]='A'+(26+str[i]-'A'+key)%26; else if(str[i]>='a' && str[i]<='z') str[i]='a'+(26+str[i]-'a'+key)%26; printf(str); }
  • 2楼网友:詩光轨車
  • 2021-02-08 21:25
char str[10]="I am a boy"; int key=4; char *p=str; while(*p!='\0') { if((*p>='A' && *p<='V') || (*p>='a' && *p<='v')) *p+=key; else if((*p>='W' && *p<='Z') || (*p>='w' && *p<='z')) *p+=key-26; }
  • 3楼网友:零点过十分
  • 2021-02-08 20:35
#include #include main() { char str[256] = {0}; short key, rc; printf("Please input text: "); fgets(str, 256, stdin); printf("Please input key: "); scanf("%d", &key); if (key > 100) { printf("key too big : 0 ~ 99"); exit(-1); } for (rc = 0; rc < strlen(str); rc++) { if (isalpha(str[rc])) str[rc] += key; } printf("Cryptograph :%s", str); getch(); }
  • 4楼网友:爱难随人意
  • 2021-02-08 20:06

13 是错的!  正确的结果是20。

循环一共执行了4次,分别是x=1时、x=2时、x=3时、x=4时。

每当进入switch的分支语句,就遇到case 0:  后没有任何执行语句,所以无论x等于几。都

直接调到 default: x+=5 处执行。

所以,四次循环,x一共加了4次5,所以答案是20。

还有,你的符合打错了,case 和 default 后面都是冒号,不是分号。

图:

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯