首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ASCII代码加密消息

使用ASCII代码加密消息
EN

Stack Overflow用户
提问于 2014-02-05 01:34:06
回答 1查看 2.2K关注 0票数 0

该问题需要按照以下规则输出输入:

代码语言:javascript
复制
1. if the input character is between A-Z, or a-z, the out put character would be
   the following letter, abc-->bcd
2. if the input is Z or z, the output would be A or a,  z->a,Z->A
3. if the input is space, then it remains the same
4. if the input is anything else, increment its ascii value by 1, and print.

这就是一个例子:

代码语言:javascript
复制
input:  abcZ ]     
output: bcdA ^

这是我的代码:

代码语言:javascript
复制
#include <iostream>
using namespace std;

int main()
{//use ASCII to get a code for input by changing a to b, b to c....z to a, space remain the same, everything else ++
    char c,d;
    int i=0;

    for(i=0;;i++)
    {
        if (('A' <= (c=cin.get()) < 'Z')||('a' <= (c=cin.get()) < 'z'))
        {
            d=c+1;

        }
        else if(c=cin.get()==32)// ascii value of space is 32
            d=c;
        else if((c=cin.get())=='Z')
            d='A';
        else if((c=cin.get())=='z')
            d='a';
        else
        {
            c++;
            d=c;
        }
        cout<<d;

    }
    cout<<endl;
    return 0;
}

这是输出:

我想的是是enter键的输出,但是我不想要enter键的输出。空格,Z和z也不能正确地转换。

有人能帮我处理密码吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-05 01:54:14

你这里有一大堆问题。以下是一些提示:

1)每次循环迭代只调用一次cin.get()。这就是:

代码语言:javascript
复制
for (...)
{
   c = cin.get();
   // do not call cin.get() beyond this point.
   // use the c variable instead

   ...
}

2)注意你的复合条件。

而不是:('A' <= c < 'Z'),您真的想要:('A' <= c && c < 'Z')

3)添加另一个条件以检查10。这是行提要字符的代码。如果检测到这一点,只需执行cout << endl

这里也有许多简化逻辑的方法。继续努力!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21567026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档