•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,233 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,778 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1463 | Replies: 9 | Solved
![]() |
Good day. My semester of c++ is coming to an end. I've got to complete a total of 7 assignments within the next 3 weeks. I've completed 5 out of the seven. I've got two left (1 has a separate thread by itself). So you can also take a look at it. This is the assignment:
C++ Program - CODE TRANSLATOR
You have been hired you to write a special code translating program. They usually use a simple letter transformation, where the alphabets sequence of letters is started at somewhere other than the letter "A".
Example:
This "extremely sophisticated" code has finally been broken by the professor and now the the students wants a code as follows:
1. Enter a key phrase ( no spaces)
2. Remove all the duplicate letters in the key phrase.
3. Remove the letters that remain in the edited key phrase from the string of the 26 alphabet letters.
4. Connect the edited key phrase to the front of the edited alphabet.
5. Use this new string to transform letters in the code to the alphabet (similar to the above example)
Example:
1. Key Phrase: DOCTORZOOSSCIENCEREVIEWS
2. Remove duplicates: DOCTRZSIENVW
3. Remove these letters from the alphabet: ABFGHJKLMPQUXY
4. Connect these two: DOCTRZSIENVWABFGHJKLMPQUXY
5. Use to decode; ABCDEFGHIJKLMNOPQRSTUMXYZ
CODE : CDMLEFB LIR AFMBLDEBK DJR ZMWW FZ IEWWK
TRANSL : CAUTION THE MOUNTAINS ARE FULL OF HILLS
Write a program called "DECODE" that will allow for the keyboard entry of the key phrase (which Mr. Brown will supply to you), read one line of code from a text file, decode it and display the translation to the screen and printer. The
text files are individual and named as follows:
c:\\f(9999).txt example: f1234.txt
Here is one way of removing duplicates from a string;
1. Enter string1 and set string2 equal to a null string.
2. Set up a nested for loop. Loop1 from 0 to 1 less than the string1 length. Loop2 from Loop1 + 1 to 1 less than the string1 length (same as the Bubble Sort loops)
3. If the letters pointed to by both loops are equal, set the Loop2 letter to an *
4. After the loops are finished, start another loop from 0 to length of string1
5. If the letter is NOT an *, then concatenate it to the end of string2.
Here's an idea:
Thanks for any ideas, jumpstart and help. Have a good day.
C++ Program - CODE TRANSLATOR
You have been hired you to write a special code translating program. They usually use a simple letter transformation, where the alphabets sequence of letters is started at somewhere other than the letter "A".
Example:
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Where H = A. R = K. A=T. G=Z.
1. Enter a key phrase ( no spaces)
2. Remove all the duplicate letters in the key phrase.
3. Remove the letters that remain in the edited key phrase from the string of the 26 alphabet letters.
4. Connect the edited key phrase to the front of the edited alphabet.
5. Use this new string to transform letters in the code to the alphabet (similar to the above example)
Example:
1. Key Phrase: DOCTORZOOSSCIENCEREVIEWS
2. Remove duplicates: DOCTRZSIENVW
3. Remove these letters from the alphabet: ABFGHJKLMPQUXY
4. Connect these two: DOCTRZSIENVWABFGHJKLMPQUXY
5. Use to decode; ABCDEFGHIJKLMNOPQRSTUMXYZ
CODE : CDMLEFB LIR AFMBLDEBK DJR ZMWW FZ IEWWK
TRANSL : CAUTION THE MOUNTAINS ARE FULL OF HILLS
Write a program called "DECODE" that will allow for the keyboard entry of the key phrase (which Mr. Brown will supply to you), read one line of code from a text file, decode it and display the translation to the screen and printer. The
text files are individual and named as follows:
c:\\f(9999).txt example: f1234.txt
Here is one way of removing duplicates from a string;
1. Enter string1 and set string2 equal to a null string.
2. Set up a nested for loop. Loop1 from 0 to 1 less than the string1 length. Loop2 from Loop1 + 1 to 1 less than the string1 length (same as the Bubble Sort loops)
3. If the letters pointed to by both loops are equal, set the Loop2 letter to an *
4. After the loops are finished, start another loop from 0 to length of string1
5. If the letter is NOT an *, then concatenate it to the end of string2.
Here's an idea:
cplusplus Syntax (Toggle Plain Text)
string stringl, string2; string2 = “ “ // making string2 a null string cout << "Enter string1; "; getline(cin, string1); for(i=0; I < string1.Iength()-1; i++) for(j=i+1; ]< string1.length();j++) { if(string1[i] ! =’ ‘ ) // skipping spaces { if (string1[i] == string1 [j]) // finding duplicates string1[j] = ‘*’; // making the duplicate letter a '*' )// end if ) // end for j // Removing the *'s, spaces and creating a new string for(i=0; i < string1.length(); i++) { if(string1[i] != '*' && stringi [i] != ' ') // checking for an asterick (*) and a space string2 += string1[i]; } // end for i cout <<string2 << endl;
I shot the sheriff....but I didn't shoot the deputy
First things first: Format your code
Nested loops.... Replace any duplicates with SPACEs. When done, compress the SPACEs out.
This one is easier than above, since you know there are no duplicates. I assume you know the relationship between letters and the value of the letter (A==65).
This you can probably figure out.
Nested loops.... Replace any duplicates with SPACEs. When done, compress the SPACEs out.
•
•
•
•
3. Remove the letters that remain in the edited key phrase from the string of the 26 alphabet letters.
This you can probably figure out.
Age is unimportant -- except in cheese
•
•
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,012
Reputation:
Rep Power: 6
Solved Threads: 68
Can you not use the string apis ?
•
•
Join Date: Jul 2005
Posts: 1,285
Reputation:
Rep Power: 9
Solved Threads: 175
3. Remove these letters from the alphabet:
declare a string containing all the letters of the alphabet.
loop through string2 and compare each letter in it with each letter in the alphabet string. Each time you find a letter from string2 in the alphabet string replace it with an asterix. Then remove the asterixes from the alphabet string to get an edited alphabet string. This should sound familiar, because it's basically, not quite, but basically, doing the same thing you've already done.
declare a string containing all the letters of the alphabet.
loop through string2 and compare each letter in it with each letter in the alphabet string. Each time you find a letter from string2 in the alphabet string replace it with an asterix. Then remove the asterixes from the alphabet string to get an edited alphabet string. This should sound familiar, because it's basically, not quite, but basically, doing the same thing you've already done.
Good day guys. So I'm pretty much done with the C++ class (which i don't need any other one). This program (as below) and I have some problems. See my first post for what the assignment is about and also, I've included the errors I get.
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <cctype> #include <iomanip> #include <cmath> #include <dos.h> #include <fstream> #include <string> #include <ctime> #include <conio.h> #include <windows.h> #define cls system("cls") #define frz system("pause"); #define yl system("color 0e"); using namespace std; //********************************** Type definitions void layout(); // declaration of display function string ReadFromFile(); // declaration of function to read from files string encrypt(string code, string text); // declaration of function to encrypt files string decrypt(string code, string text); // declaration of function to decrypt files string codeCreation(string string1, char alphabet[26]); // declaration of function create code string numericalValue(string s); // find the number value //********************************** Function Prototypes //********************************** Main Function //declaration of string variables string AlphabetCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string converUppercase(string strn1); string s1, s3, original, s7 = "ETRERTRHFG"; string string2; string string3; //the main function were everything starts and ends int main() { //declaration of the original alphabet char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; start: //initialization of string string s2 = ""; // making string2 a null string //get string from user cout << "Enter string to create the decryption alphabet code : "; cin >> s1; cout << '\n'; original = s1; //convert string to upper case s7 = converUppercase(s1); //create encryption code s3 = codeCreation(s7, alphabet); //encrypt the file s1 = encrypt(s3, s7); //decrypt the file decrypt(s3, s1); cout << '\n'; //clear the user screen clrscr(); //read from files ReadFromFile(); //prove user interaction layout(); //paused for 3 seconds sleep(3); //allow user to decode a file again int choice; cout << '\n'<< '\n'; cout << "to decode another file press 2, or 9 to exit" << '\n'; cin >> choice; switch (choice){ case 2: goto start; case 9: exit (1); default: cout<<"invalid choice"; } getch(); return 0; }//end main //the folowing is the initialization of the code us in the decrypt function string codeCreation(string string1, char alphabet[26]){ //initialization of string string3= ""; for(int i=0; i < string1.length()-1; i++) for(int j=i+1; j< string1.length(); j++) { if(string1[i] !=' ' ) // skipping spaces { if (string1[i] == string1[j]) {// finding duplicates // if (string1[j] !='*') //string3 += string1[j]; string1[j] = '*'; } // making the duplicate letter a '*' }// end if } // end for j //cout << "string1 = " << string1; // Removing the *'s, spaces and creating a new string for(int i=0; i < string1.length(); i++) { if(string1[i] != '*' && string1[i] != ' ') // checking for an asterick (*) and a space string2 += string1[i]; //else //string3 += string1[i]; } // end for i //check do + for(int i=0; i < 26; i++) { for(int j=0; j < string2.length(); j++) if(alphabet[i] == string2[j]) alphabet[i] = '+'; } // end for i //create string3 for(int i=0; i < 26; i++) { if(alphabet[i] != '+') string3 += alphabet[i]; } // end for i cout << '\n'; //cout << string1; cout << '\n'; string3 = string2 + string3; //cout << "string 3 = " << string3; cout << '\n'; //cout << "string 2 = " << string2; cout << '\n'; return string3; }//end function //the following function will encrypt a file string encrypt(string code, string text) { //cout << '\n'; // cout << "your original text is first converted to uppercase to give " // << "the result below : "<<'\n'<<"Uppercase Original text : " // << text<<'\n'; for(int i=0; i < text.length(); i++) { if (text[i] == code[0] ) text[i] = 'A'; else if (text[i] == code[1] ) text[i] = 'B'; else if (text[i] == code[2] ) text[i] = 'C'; else if (text[i] == code[3] ) text[i] = 'D'; else if (text[i] == code[4] ) text[i] = 'E'; else if (text[i] == code[5] ) text[i] = 'F'; else if (text[i] == code[6] ) text[i] = 'G'; else if (text[i] == code[7] ) text[i] = 'H'; else if (text[i] == code[8] ) text[i] = 'I'; else if (text[i] == code[9] ) text[i] = 'J'; else if (text[i] == code[10] ) text[i] = 'K'; else if (text[i] == code[11] ) text[i] = 'L'; else if (text[i] == code[12] ) text[i] = 'M'; else if (text[i] == code[13] ) text[i] = 'N'; else if (text[i] == code[14] ) text[i] = 'O'; else if (text[i] == code[15] ) text[i] = 'P'; else if (text[i] == code[16] ) text[i] = 'Q'; else if (text[i] == code[17] ) text[i] = 'R'; else if (text[i] == code[18] ) text[i] = 'S'; else if (text[i] == code[19] ) text[i] = 'T'; else if (text[i] == code[20] ) text[i] = 'U'; else if (text[i] == code[21] ) text[i] = 'V'; else if (text[i] == code[22] ) text[i] = 'W'; else if (text[i] == code[23] ) text[i] = 'X'; else if (text[i] == code[24] ) text[i] = 'Y'; else if (text[i] == code[25] ) text[i] = 'Z'; }//END FOR cout << text << " "; return text; } //the following function will convert from lower case to uppercase string converUppercase(string strn1) { cout << " "; for(int i=0; i < strn1.length(); i++) if(strn1[i] == 'a') strn1[i] = 'A'; else if(strn1[i] == 'b') strn1[i] = 'B'; else if(strn1[i] == 'c') strn1[i] = 'C'; else if(strn1[i] == 'd') strn1[i] = 'D'; else if(strn1[i] == 'e') strn1[i] = 'E'; else if(strn1[i] == 'f') strn1[i] = 'F'; else if(strn1[i] == 'g') strn1[i] = 'G'; else if(strn1[i] == 'h') strn1[i] = 'H'; else if(strn1[i] == 'i') strn1[i] = 'I'; else if(strn1[i] == 'j') strn1[i] = 'J'; else if(strn1[i] == 'k') strn1[i] = 'K'; else if(strn1[i] == 'l') strn1[i] = 'L'; else if(strn1[i] == 'm') strn1[i] = 'M'; else if(strn1[i] == 'n') strn1[i] = 'N'; else if(strn1[i] == 'o') strn1[i] = 'O'; else if(strn1[i] == 'p') strn1[i] = 'P'; else if(strn1[i] == 'q') strn1[i] = 'Q'; else if(strn1[i] == 'r') strn1[i] = 'R'; else if(strn1[i] == 's') strn1[i] = 'S'; else if(strn1[i] == 't') strn1[i] = 'T'; else if(strn1[i] == 'u') strn1[i] = 'U'; else if(strn1[i] == 'v') strn1[i] = 'V'; else if(strn1[i] == 'w') strn1[i] = 'W'; else if(strn1[i] == 'x') strn1[i] = 'X'; else if(strn1[i] == 'y') strn1[i] = 'Y'; else if(strn1[i] == 'z') strn1[i] = 'Z'; else strn1[i] = strn1[i]; return strn1; } //the following function will decode a file string decrypt(string code, string text) { //initialization of string string answer = " "; int hold = 99, temp = 3; //cout << '\n'; //cout << "decry befor CODE " << code; //cout << '\n'; //cout << "decrypted text " << text << '\n'; // the following is a loop that read all characters from the text input file for(int j=0; j < text.length(); j++) { //ensure that j is not repeated if(temp == j) {} else // the following is a loop that read all characters from the text input file for(int c=code.length()-1; c >=0 ; c--) { //cout << temp << " diff temp = " << "j " << j << '\n'; if(j == temp) cout << temp << " temp = " << "j " << j << '\n'; //break;//j = temp++; //start the encryption if(AlphabetCode[c] == text[j] && hold !=j) { text[j] = code[c]; //cout << " j code " << j; //cout << " c code " << c ; //cout << " text code " << code[c] << '\n'; break; } else //do no changes text[j] = text[j]; hold = temp; } //sleep(1); //check that j has change if(temp != j){ temp = j; } else cout << " same "; // hold = j; } //output to screen cout << " " << text; //return string return text; } //function that reads information from file string ReadFromFile(){ //declaration of strings string TextFromFile, sConvertToUpper, printToFile, encryptFile; //the following provide interaction will files on the computer ofstream outfile ("decodeFile.txt", ios::out); ofstream outdecode ("output.txt", ios::out); ifstream inencrypt ("output.txt", ios:: in); ifstream infile ("f1234.txt", ios:: in); //check if file was open if (!infile) { cout << "Error opening file"; exit (1); } //check if file was open if (!inencrypt) { cout << "Error opening file"; exit (1); } //check if file was open if (!outdecode) { cout << "Error opening file for printing"; exit (1); } //check if file was open if (!outfile) { cout << "Error opening file for printing"; exit (1); } //read all from the text file while (infile >>TextFromFile) { //conver each word in the file f1234.txt to uppercase sConvertToUpper = converUppercase(TextFromFile); //encrypt the ininformation read from f1234.txt encryptFile = encrypt(s3, sConvertToUpper); //print the encrypted information to a file called output.txt outdecode << encryptFile << " "; //decrypt the information that was encrypt printToFile = decrypt(s3, encryptFile); //print the decrypt information to a file called decodeFile.txt outfile << printToFile << " "; } //cout << TextFromFile << " "; //conver each word in the file f1234.txt to uppercase sConvertToUpper = converUppercase(TextFromFile); //encrypt the ininformation read from f1234.txt encryptFile = encrypt(s3, sConvertToUpper); //print the encrypted information to a file called output.txt outdecode << encryptFile << " "; //decrypt the information that was encrypt printToFile = decrypt(s3, encryptFile); //print the decrypt information to a file called decodeFile.txt outfile << printToFile << " "; //return the string return TextFromFile; } //the following function provides interaction for the user void layout() { //clear the screen clrscr(); cout << '\n'; clrscr(); //display the headder information cout << "****************************************************************************"; cout << '\n'; cout << "******* THIS PROGRAM WILL READ FROM THE FILE f1234.txt *******"; cout << '\n'; cout << "******* THEN *******"; cout << '\n'; cout << "******* ENCRYPT THE FILE AND PRINT TO output.txt *******"; cout << '\n'; cout << "******* THEN FINALLY *******"; cout << '\n'; cout << "******* DECRYPT THE ENCRYPTED FILE AND PRINT TO decodeFile.txt *******"; cout << '\n'; cout << "****************************************************************************"; cout << '\n'; //display the original string of characters you entered tfor code creation cout << "your original alphabet text is : " << original; cout << '\n'<< '\n'<< '\n' ; //display the code cout << "the decrytion alphabet code is below: "; cout << '\n'<< "code : "<< s3 << '\n' ; //declaration of string variables string readFromf1234,readfromoutput, readFromdecodeFile; //provide interaction with files from the computer ifstream readdecodeFile ("decodeFile.txt", ios:: in); ifstream readoutput ("output.txt", ios:: in); ifstream readf1234 ("f1234.txt", ios:: in); //check if file was open if (!readf1234) { cout << "Error opening file f1234.txt"; exit (1); } //check if file was open if (!readoutput) { cout << "Error opening file output.txt"; exit (1); } //check if file was open if (!readdecodeFile) { cout << "Error opening file decodeFile.txt"; exit (1); } //paused for 2 seconds sleep(2); cout << '\n'<< '\n'<< '\n'; cout << "the following is a copy of the original file read from the file f1234.txt"; cout << '\n' << '\n'; //read all input from f1234.txt while (readf1234 >>readFromf1234) { cout << readFromf1234 << " "; } cout << readFromf1234 << " "; cout << '\n'; //paused for 2 seconds sleep(2); cout << '\n'<< '\n'<< '\n'; cout << "the following is a copy of the file that was encrypted in the file output.txt"; cout << '\n'<< '\n'; //read all input from output.txt while (readoutput >>readfromoutput) { cout << readfromoutput << " "; } cout << readfromoutput << " "; cout << '\n'; //paused for 2 seconds sleep(2); cout << '\n'<< '\n'<< '\n'<< '\n'; cout << "the following is a copy of the file that was decoded in the file decodeFile.txt"; cout << '\n'<< '\n'; //read all input from decodefile.txt while (readdecodeFile >>readFromdecodeFile) { cout << readFromdecodeFile << " "; } cout << readFromdecodeFile << " "; cout << '\n'; }//end layout function


