#include <bits/stdc++.h>
using
namespace
std;
// Function for encryption
string encrypted_string(string strg)
{
string EncryptedString = { strg[0] };
for
(
int
t = 1; t < strg.length(); t++) {
if
(t % 2 == 1) {
EncryptedString.push_back(
strg[t]);
// At odd position, append the
// next character
}
else
{
EncryptedString.insert(
0, 1, strg[t]);
// At Even position, prepend
// the next character
}
}
// print the resultant string
return
EncryptedString;
}
// Function for decryption
string decrypted_string(string strg)
{
// Check the divisibility of length of string by 2
if
(strg.length() % 2 != 0) {
string DecryptedString = { strg[0] };
for
(
int
t = 1; t < strg.length(); t++) {
DecryptedString.push_back(
strg[strg.length() - 1
- t]);
// First add the characters from
// the back
DecryptedString.push_back(
strg[t]);
// from the front
}
reverse(
DecryptedString.begin(),
DecryptedString.end());
// Reverse the final
// string to get output
return
DecryptedString;
}
else
{
string reverse_strg = strg;
reverse(
reverse_strg.begin(),
reverse_strg
.end());
// In case of even length, reverse
// the given string first
string DecryptedString = { reverse_strg[0] };
// Looping over the reversed string
for
(
int
t = 1; t < reverse_strg.length(); t++) {
DecryptedString.push_back(
reverse_strg[reverse_strg.length() - 1
- t]);
// First add the
// characters from the
// back
DecryptedString.push_back(
reverse_strg[t]);
// and then from the front
}
reverse(
DecryptedString.begin(),
DecryptedString.end());
// Reverse the final
// string to get output
return
DecryptedString;
}
}
// Testing the algorithm with Driver Code
int
main()
{
cout << encrypted_string(
"geeks"
) << endl;
cout << encrypted_string(
"geeksforgeeks"
) << endl;
cout << encrypted_string(
"strive"
) << endl;
cout << encrypted_string(
"vikaschitturi"
) << endl;
cout << encrypted_string(
"padmachitturi"
) << endl;
cout << encrypted_string(
"hackerrank"
) << endl;
cout << encrypted_string(
"hackerearth"
) << endl;
cout << encrypted_string(
"codechef"
) << endl;
cout << encrypted_string(
"IamVikas"
) << endl;
cout << encrypted_string(
"HelloHowareyou"
) << endl;
cout << encrypted_string(
"whatareyoudoing"
) << endl;
cout << decrypted_string(
"segek"
) << endl;
cout << decrypted_string(
"segosegekfrek"
) << endl;
cout << decrypted_string(
"vrstie"
) << endl;
cout << decrypted_string(
"iuthskviacitr"
) << endl;
cout << decrypted_string(
"iuthadpamcitr"
) << endl;
cout << decrypted_string(
"nrechakrak"
) << endl;
cout << decrypted_string(
"hreechakrat"
) << endl;
cout << decrypted_string(encrypted_string(
"IamVikas"
))
<< endl;
cout << decrypted_string(
encrypted_string(
"HelloHowareyou"
))
<< endl;
return
0;
}