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

Problem A: Secret text Time limit:2 sec Problem Description Write a program to t

ID: 3915023 • Letter: P

Question

Problem A: Secret text Time limit:2 sec Problem Description Write a program to translate a secret text. The text is encrypted by the following rules. First if the text is a lowercase letter, we translate it into an uppercase letter, and the first letter 'a' is translate into the last letter "Z', the second letter 'b' is translated into the second to the last letter 'Y", and so on. If the text is an uppercase letter, we translate it into a lowercase letter similarly. If we see a digit, we will translate it into the next digit, and the only exception is "9, which will be translated into "O. On all other cases, the text is not changed. Input File Format The input is lines of text. Output Format The output is the text after translation. Example Sample Input: This is a test line. We have roughly 59 characters in this example. Sample Output: gSRH RH Z GVHG ORMV. dV SZEV ILFTSOB 60 XSZIZXGVIH RM GSRH VCZNKOV.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

#include<iostream>

using namespace std;

//method to convert an input string to secret text

string convert(string input){

                string output=""; //output string

                //auxiliary variables needed for the calculation of secret text

                int index;

                char replacement_char;

                //looping through all characters in the input string

                for(int i=0;i<input.length();i++){

                                char c=input[i]; //getting current character

                                if(c>='a' && c<='z'){

                                                //lowercase

                                                index=c-'a'; //find difference from character 'a'

                                                replacement_char='Z'-index; //decrementing the difference from 'Z'

                                                output+=replacement_char; //appending to output text

                                }else if(c>='A' && c<='Z'){

                                                //upper case, performing the above techniques (after inversing characters)

                                                index=c-'A';

                                                replacement_char='z'-index;

                                                output+=replacement_char;

                                }else if(c>='0' && c<='9'){

                                                //digit

                                                replacement_char=c+1; //next character

                                                if(replacement_char>'9'){

                                                                //wrapping around in case the original digit was 9

                                                                replacement_char='0';

                                                }

                                                output+=replacement_char;

                                }else{

                                                //other character, no changes

                                                output+=c;

                                }

                }

                return output;

}

int main(){

                string line;

                string input="";

                cout<<"Enter text, press ctrl+Z to stop: "<<endl;

                //getting input until EOF (press ctrl+Z to stop)

                while(getline(cin,line)){

                                input+=line+" ";

                }

                cout<<"Translated text:"<<endl;

                //converting and displaying secret text

                cout<<convert(input);

}

/*OUTPUT*/

Enter text, press ctrl+Z to stop:

This is a test line

We have roughly 59 characters in this example.

^Z

Translated text:

gSRH RH Z GVHG ORMV

dV SZEV ILFTSOB 60 XSZIZXGVIH RM GSRH VCZNKOV.

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