The name of the file must be encrypt.cpp Write a program that prompts the user t
ID: 3726452 • Letter: T
Question
The name of the file must be encrypt.cpp Write a program that prompts the user to enter a text to be encrypted, parse the text, process it and display the text encrypted using the following algorithm: All punctuations are discarded, this includes commas(), dots(.), question marks(?), exclamation marks(), semicolons), and colons( -If the string starts with a vowel, move the last 3 letters to the beginning of the string then add the ‘ symbol at the beginning If the string starts with a consonant, move the first 3 letters to the end of the string and add the ' symbol to the end For any string that is of length less than or equal to 3, reverse the order of the string and add a to the beginning and %, to the end of the string You can read the input as a C-string or a string object, however, using string class for this homework is recommended. A tokenizing function should be used first, it will use the space as a delimiter. For example: “Hello, everyone! This is: COSC-1436, SP18" has 5 space delimiters 6 tokens Sample Run Enter a sentence with 10 or less words and it will be encrypted: Hello, everyone! This is: COSc-1436, SP18 The unencrypted tokens are: Hello everyone This is COSC-1436 SP18 The encrypted tokens are: loHel@ oneevery sThi@ #5 i% C-1436C0S@ 8SP10 The encrypted text for this input string is: LoHeL@oneeverysThi@#sisc-1436COS@8SP1@Explanation / Answer
#include<iostream>
#include<sstream>
using namespace std;
void encrypt(string &token);
int main()
{
string line;
string tokens[12];
stringstream ss;
cout << "Enter a sentence with 10 or less words and it will be encrypted: " << endl;
getline(cin, line);
ss << line;
//remove all punctuations an d get the array of strings
int i = 0;
while (!ss.eof())
{
ss >> tokens[i++];
//cout << tokens[i] << endl;
}
for (int j = 0; j < i; j++)
{
for (int k = 0; k < tokens[j].length(); k++)
{
if (tokens[j][k] == ',' || tokens[j][k] == ':' || tokens[j][k] == ';' || tokens[j][k] == '!' || tokens[j][k] == '.')
{
tokens[j].erase(k);
}
}
}
//display enencrypted tokens ares
cout << "The unencrypted tokens are: ";
for (int j = 0; j < i; j++)
{
cout << tokens[j] << endl;
//call encrypt function
encrypt(tokens[j]);
}
//display enencrypted tokens ares
cout << "The encrypted tokens are: ";
for (int j = 0; j < i; j++)
{
cout << tokens[j] << endl;
}
//add all the foken to line
//first clear the line content and copy encrypted tokens into it.
line.clear();
for (int j = 0; j < i; j++)
{
line += tokens[j];
}
cout <<endl<<endl<< "The encrypted text for this input string is: " << endl;
cout << line << endl;
}
void encrypt(string &token)
{
string s;
if (token.length() < 3)
{
s += '#';
for(int i = token.length()-1; i>=0;i--)
s += token[i];
s += '%';
token = s;
}
else
{
if (token[0] == 'a' || token[0] == 'e' || token[0] == 'i' || token[0] == 'o' || token[0] == 'u' || token[0] == 'A' || token[0] == 'E' || token[0] == 'I' || token[0] == 'O' || token[0] == 'U')
{
//first letter is vowel, move last three letters to beginning of string with * in at the start
s += '*';
for (int i = token.length() - 3; i < token.length(); i++)
{
s += token[i];
}
for (int i = 0; i < token.length() - 3; i++)
{
s += token[i];
}
token = s;
}
//starts with consonent
else
{
for (int i = 3; i < token.length(); i++)
{
s += token[i];
}
for (int i = 0; i < 3; i++)
{
s += token[i];
}
//add @ at the end of the string
s += '@';
token = s;
}
}
}
/*output
Enter a sentence with 10 or less words and it will be encrypted:
Hello, everyone! This is: COSC-1436, SP18
The unencrypted tokens are: Hello
everyone
This
is
COSC-1436
SP18
The encrypted tokens are: loHel@
*oneevery
sThi@
#si%
C-1436COS@
8SP1@
The encrypted text for this input string is:
loHel@*oneeverysThi@#si%C-1436COS@8SP1@
*/