C/C++

Задачка по программированию на c++

Легко и просто:
 #include 
#include
#include

using namespace std;

int main() {
const char k = 3;
string s;
getline(cin, s);
for (size_t i = 0; i < s.size(); i++) {
if (isalpha(s[i])) {
char c = s[i] + k;
if (!isalpha(c)) c -= 26;
s[i] = c;
}
}
cout
Олег Кащеев
Олег Кащеев
54 053
Лучший ответ
 #include  
#include
#include

using namespace std;

struct CaesarCipher {
static string encode(const string& line, int k) {
k %= length;
if (k 0 && isalpha(tmp)) {
if (tmp > Z) {
tmp += k;
if (tmp > z) {
tmp -= length;
}
} else {
tmp += k;
if (tmp > Z) {
tmp -= length;
}
}
}
ch = tmp;
}
return text;
}
private:
static const int length = 26;
static const int Z = 'Z';
static const int z = 'z';
};

int main() {
int k = 3;
string line;
getline(cin, line);
auto cipher = CaesarCipher::encode(line, k);
cout
 #include  

int main()
{
char ch, base; int k = 3;
while (std::cin.get(ch),ch!='\n')
{
((ch >= 'A' && ch = 'a' && ch
Контроля за количеством символов и их кодами нет. Если надо, скажи, добавлю, если буду на связи.

 #include  
#include

const std::string az = "abcdefghijklmnopqrstuvwxyz";
const std::string AZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int is_az(const char c)
{
for(int x = 0; x < 26; x++)
if(az[x] == c)
return x+1;
return 0;
}

int is_AZ(const char c)
{
for(int x = 0; x < 26; x++)
if(AZ[x] == c)
return x+1;
return 0;
}

std::string str_forvard(std::string str, int k)
{
std::string rc = "";
int pos;

for(int x = 0; x < str.length(); x++)
{
pos = is_az(str.at(x));
if(pos)
{
pos += (k - 1);
if(pos < 0)
rc += az.at(pos+26);
else if(pos >= 26)
rc += az.at(pos - 26);
else
rc += az.at(pos);
}
else
{
pos = is_AZ(str.at(x));
if(pos)
{
pos += (k - 1);
if(pos < 0)
rc += AZ.at(pos+26);
else if(pos >= 26)
rc += AZ.at(pos - 26);
else
rc += AZ.at(pos);
}
else
rc += str.at(x);
}
}

return rc;
}

std::string str_back(std::string str, int k)
{ // эту функцию можно удалить. Она для наглядности, если надо мотать алфавит назад.
std::string rc = "";
int pos;

for(int x = 0; x < str.length(); x++)
{
pos = is_az(str.at(x));
if(pos)
{
pos += (-k - 1);
if(pos < 0)
rc += az.at(pos+26);
else if(pos >= 26)
rc += az.at(pos - 26);
else
rc += az.at(pos);
}
else
{
pos = is_AZ(str.at(x));
if(pos)
{
pos += (k - 1);
if(pos < 0)
rc += AZ.at(pos+26);
else if(pos >= 26)
rc += AZ.at(pos - 26);
else
rc += AZ.at(pos);
}
else
rc += str.at(x);
}
}

return rc;
}

int main()
{
std::string str1,str2;
int k = 3;

getline(std::cin,str1);
str2 = str_forvard(str1, k);

std::cout