Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ with Strings NEED HELP URGENT The SecretCoding is an algorithm that you deci

ID: 3798817 • Letter: C

Question

C++ with Strings NEED HELP URGENT

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

SecretEncryption.cpp :

#include<stdio.h>

#include<iostream.h>

#include<string.h>

#include<conio.h>

#include<ctype.h>

const int NUM_OF_TOKENS = 10;

char tokens[NUM_OF_TOKENS][20];

char encryption[NUM_OF_TOKENS][20]={""};

int isvowel(char);

int tokenize(char myString[]){

int k=0,p=0;

for(int i=0;i<strlen(myString);i++){

if(myString[i] == ' '){

k++;

p = 0;

}

else

tokens[k][p++] = myString[i];

}

return k+1;

}

void vowel_encryption(char s[],int i)

{

char res[10];

res[0] = '#';

int k=1;

for(int j=1;j<=3;j++)

res[k++] = s[strlen(s)-j];

for(int p=0;p<strlen(s)-3;p++)

res[k++] = s[p];

strcpy(encryption[i],res);

}

void consonant_encryption(char s[],int i)

{

char res[10];

int k=1;

for(int j=3;j<strlen(s);j++)

res[k++] = s[j];

for(int p=0;p<3;p++)

res[k++] = s[p];

res[k] = '$';

strcpy(encryption[i],res);

}

void short_encryption(char s[],int i)

{

char res[10];

res[0] = '%';

int k = 1;

for(int j=strlen(s)-1;j>=0;j--){

res[k++] = s[j];

}

res[k] = '%';

strcpy(encryption[i],res);

}

int main ()

{

clrscr();

// TODO: prompt the user to input a string

char str[100];

int i,count,j;

fflush(stdout);

cout<<"Enter input String:"<<endl;

fflush(stdin);

gets(str);

// TODO: call the tokenize function

count = tokenize(str);

// TODO: display the tokens

for(i=0;i<count;i++)

cout<<tokens[i]<<endl;

// 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

for(i=0;i<count;i++){

if(strlen(tokens[i])<=3){

short_encryption(tokens[i],i);

}

else if(isvowel(tokens[i][0]))

vowel_encryption(tokens[i],i);

else

consonant_encryption(tokens[i],i);

}

// TODO: display the encrypted strings (the results)

cout<<"The encrypted text for this input string is:";

for(i=0;i<count;i++)

cout<<encryption[i]<<" ";

getch();

return 0;

}

int isvowel(char ch){

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

return 1;

else if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

return 1;

else

return 0;

}

Sample Input & output :

Hello

everyone

This

is

COSC-1436

The encrypted text for this input string is: loHel$ #oneevery sThi$ %si% C-1436COS$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote