C++ with strings, need help with coding. The SecretCoding is an algorithm that y
ID: 3799059 • Letter: C
Question
C++ with strings, need help with coding.
The SecretCoding is an algorithm that you decided to make-up to encrypt text. In this algorithm, each string is encrypted as follows:
- 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 end of the string.
The input text can be of any length. The text can be tokenized using different delimiters. In this homework the delimiter you will use is the blank( ). What this means is that if as an input you have the sentence: Hello, everyone! This is: COSC-1436. The tokens that comprise this sentence based on the delimiter you have and after discarding the punctuations, are:
Hello
everyone
This
is
COSC-1436
The encrypted text for this input string is: loHel$ #oneevery sThi$ %si% C-1436COS$
Use the string class to implement this algorithm. The program should prompt the user to enter a text to be encrypted, parse the text, process it and finally display the text encrypted based on the SecretCoding algorithm.
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int NUM_OF_TOKENS = 10;
int tokenize(string myString, string strArray[NUM_OF_TOKENS]){
istringstream iss(myString);
string token;
int i = 0;
while (getline(iss, token, ' '))
{
strArray[i] = token;
i++;
}
return i;
}
int main ()
{
// TODO: prompt the user to input a string
// TODO: call the tokenize function
// TODO: display the tokens
// TODO: call a function to encrypt stings that starts with a vowel
// TODO: call a function to encrypt stings that starts with a consonant
// TODO: call a function to encrypt short strings
// TODO: display the encrypted strings (the results)
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int NUM_OF_TOKENS = 10;
int tokenize(string myString, string strArray[NUM_OF_TOKENS]){
istringstream iss(myString);
string token;
int i = 0;
while (getline(iss, token, ' '))
{
strArray[i] = token;
i++;
}
return i;
}
string removePunctuation(string text)
{
//All punctuations are discarded, this includes commas(,), dots(.), question marks(?),
//exclamation marks(!), semicolons (;), and colons(:).
string output = "";
for(int i = 0; i < text.length(); i++)
{
char ch = text.at(i);
if(ch != ',' && ch != '.' && ch != '?' && ch != '!' && ch != ';' && ch != ':')
output += ch;
}
return output;
}
string encryptShortStrings(string text)
{
//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 end of the string.
string output = "";
text = removePunctuation(text);
for(int i = text.length()-1; i >= 0; i--)
output += text.at(i);
text = '%' + output + '%';
return text;
}
string encryptVowelStartString(string text)
{
string output = "";
text = removePunctuation(text);
if(text.length() <= 3)
return encryptShortStrings(text);
//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.
output = '#' + text.substr(text.length()-3, 3) + text.substr(0, text.length()-3);
return output;
}
string encryptConsonantStartString(string text)
{
string output = "";
text = removePunctuation(text);
if(text.length() <= 3)
return encryptShortStrings(text);
//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.
output = text.substr(3) + text.substr(0, 3) + '$';
return output;
}
int main ()
{
string text;
// TODO: prompt the user to input a string
cout<<"Enter a string: ";
getline(cin, text);
// TODO: call the tokenize function
string tokensArray[NUM_OF_TOKENS];
int numOfTokens = tokenize(text, tokensArray);
// TODO: display the tokens
for(int i = 0; i < numOfTokens; i++)
cout<<tokensArray[i]<<endl;
cout<<endl;
string encryptedString = "";
for(int i = 0; i < numOfTokens; i++)
{
char ch = tokensArray[i].at(0);
// TODO: call a function to encrypt short strings
if(tokensArray[i].length() <= 3)
encryptedString += encryptShortStrings(tokensArray[i]);
// TODO: call a function to encrypt stings that starts with a vowel
else if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I'
||ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
encryptedString += encryptVowelStartString(tokensArray[i]);
// TODO: call a function to encrypt stings that starts with a consonant
else
encryptedString += encryptConsonantStartString(tokensArray[i]);
}
// TODO: display the encrypted strings (the results)
cout<<encryptedString<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.