D
D
DDD2019-02-26 20:29:22
C++ / C#
DDD, 2019-02-26 20:29:22

Why is the space replaced with a W?

#include <iostream>
#include <string>
#include<algorithm>
#include<cctype>

using namespace std;

string space (string text)
{
     text.erase(remove(text.begin(),text.end(),' '),text.end());return text;
}

string encrypt(string text, int key)
{
    string result = "";
    space(result);
    for (auto i = text.begin(); i!=text.end(); i++)
    {
        if (isupper(*i))
            result += (*i + key - 65) % 26 + 65;

        else
            result += (*i + key - 97) % 26 + 97;
    }
return result;
}
int main()
{
string text ;
int key;
getline (cin, text);
cin >> key;
cout << "Text : " << text;
cout << "\nShift: " << key;
cout << "\nCipher: " << encrypt(text, key)<<endl;
return 0;
}

help me write the code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2019-02-26
@Mercury13

Space is not isupper.
The calculation here is in signed type, because There are two methods of division with a remainder, x86 has a built-in one where the sign of the remainder equals the sign of the dividend (and the incomplete quotient is the result of rounding towards zero). It was he who was subsequently codified in C99. Means, This means that the result of the transformation will be less than . For example, 'W'. Oh yes. In C, you can write . *i + key - 97 < 0
(*i + key - 'a') % 26 + 'a'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question