This is a caesar encryption code, please help me with a code to reverse this operation (decrept it) in C++
This is a caesar encryption code, please help me with a code to reverse this operation (decrept it) in C++
string encrypt(string text, int s)
{
string result = "";
// traverse text
for (int i=0;i<text.length();i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
if (isupper(text[i]))
result += char(int(text[i]+s-65)%26 +65);
// Encrypt Lowercase letters
else
result += char(int(text[i]+s-97)%26 +97);
}
// Return the resulting string
return result;
}
int main()
{
string text;
int s;
cin>> text;
cin >> s;
cout << "Text : " << text;
cout << "\nShift: " << s;
cout << "\nCipher: " << encrypt(text, s);
return 0;
}
Step by step
Solved in 2 steps