# Cryptography -- Write a code in C++ language that can take two inputs from user

Cryptography
Write a code in C++ language that can take two inputs from user:
a string of letters (A-Z, Capital letters only)
and a digit value for key
(Optional: you can restrict the value of k, if you have any difficulty; e.g. you can restrict the value of k from 1 to 20)
Program can encrypt user given string using the formula; f(p)=(p+k) mod 26, and display the encrypted message on screen.

Note: (Deal with spaces; space should be replaced with space exactly)
Submit Assignment in word document. In case of code file will be uploaded, marks will be deducted.

Solution:

#include<iostream>
#include<string>
using namespace std;
bool checkString(string);
string encryptString(string,int);

void main() {
string temp;
int test = 0;
int K = 0;

do {
cout << "Enter String == ";
getline(cin, temp);
if (checkString(temp)) {
test = 1;
}
else {
cout << "invalid input try again" << endl;
system("pause");
system("cls");
}

} while (test != 1);

test = 0;
do {
cout << "Enter the value of K from 1 to 20 == ";
cin >> K;
if (K > 0 && K <= 20) {
test = 1;
}
else
{
cout << "invalid input try again" << endl;
system("pause");
system("cls");
}
} while (test != 1);

string enc = encryptString(temp, K);
cout << enc;

}

bool checkString(string temp) {
for (int i = 0;i < temp.length();i++) {
if ((int(temp[i]) >= 65 && int(temp[i]) <= 91) || int(temp[i]) == 32 || temp[i]== '\0') {
}
else{
return false;
}
}
return true;
}

string encryptString(string temp,int k) {
//f(p) = (p + k) mod 26,
for (int i = 0;i < temp.length();i++) {
if (int(temp[i]) == 32 || temp[i] == '\0') {
}
else {
int p = int(temp[i]);
p = p + k;
p = p % 26;
temp[i] = char(p);
}
}
return temp;
}