cryptography-utils/shift.cc

32 lines
551 B
C++

#include <iostream>
#include <cctype>
int main(int argc, char** argv)
{
char* text;
int key;
if(argc < 3)
{
std::cout << "Usage: " << argv[0] << " <plain text> <key>" << std::endl;
return 0;
}
text = argv[1];
key = atoi(argv[2]);
for(int i = 0; text[i] != 0; ++i)
{
if(text[i] == ' ')
{
std::cout << ' ';
continue;
}
// has to be int, because the result can sometimes be > 127
int c = std::tolower(text[i])+key;
if(c < 'a') c+=26;
if(c > 'z') c-=26;
std::cout << (char)c;
}
std::cout << std::endl;
return 0;
}