/* Chris Mooney COS 161 Tues. Thurs. 4:00 to 5:15 Assignment 0.1 t_editor.cpp */ /* This program will allow the user to grab a line from a file and edit that line a number of different ways. The user can input what they want the file name to be when they print it out to file. */ /**********************************************************************************************/ # include // Needed for the basic i/o operations # include // Used for the file reading operations # include // Needed for the string functions # include // Used because of the toupper function /**********************************************************************************************/ main() { int x = 1 , ptr = 0; // Initilizations char yn , file[21] , choice , original[81] , now[81]; void replace(char[]); // Function prototype for replace void find (char[] , int &); // Function prototype for find void insert (char[] , int &); // Function prototype for insert void erase (char[] , int &); // Function prototype for delete void print (char[] , char[]); // Function prototype for print cout << "This program will allow you to grab a line from a file and edit that line a \n" << "number of different ways. you can input what you want the input file name to\n" << "be\n"; while (x == 1) // While x equals one keep looping { cout << "Would you be interested in continuing?(y/n)\n"; cin >> yn; // Asks the user if they would like to continue yn = (char) toupper(yn); // Changes the letter to uppercase switch (yn) // Use yn in the switch statement { case 'Y': // if its yes x = 0; // Break the loop break; // Break the switch case 'N': // If its no return 0; // End the program break; // Break the switch default: // Anything else cout << "Incorrect input!! Please enter y or n\n"; break; // Incorrect input loop again } } cout << "what would you like the input file to be (include the extension) ?\n"; cin >> file; ifstream qin(file , ios::in); // Creates the infile qin.getline(original , 81 , '\n'); // Grabs the line from the file strcpy (now , original); // Copies original into now cout << "The original string reads\n ( " << original << " ).\n"; while (choice != 'P') { cout << "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n" << "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°³ Make a choice ³°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°³ ³°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°³ R = Replace ³°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°³ F = Find ³°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°³ I = Insert ³°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°³ D = Delete ³°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°³ P = Print ³°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º\n" << "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º\n" << "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n"; cin >> choice; choice = (char) toupper(choice); // Changes the letter to uppercase switch (choice) // Runs the switch on choice { case 'R': // If r replace(now); // Call to function replace break; // End case case 'F': // If f find(now , ptr); // Call to function find break; // End case case 'I': // If i insert(now , ptr); // Call to function insert break; // End case case 'D': // If d erase(now , ptr); // Call to function erase break; // End case case 'P': // If p print(original , now); // Call to function print break; // End case default: // Anything else cout << "Please enter again\n"; break; // End case } } return 0; // Ends the program } /********************************************************************************************** ********************************* REPLACE FUNCTION ****************************************** **********************************************************************************************/ void replace(char now[]) // Definition for replace { char temp1[81] , temp2[81] , word1[21] , word2[21]; int y = 0 , p , ptr; cout << "What word do you want to replace?\n"; cin >> word1; cout << "What do you want to replace " << word1 << " with?\n"; cin >> word2; for (int i = 0 ; i <= 80 ; i++) // Go through the whole line { if (strncmp((now + i) , word1 , strlen(word1)) == 0) // Search for the word ptr = i; // If the word is found make a pointer to the spot (fake ptr) } strncpy (temp1 , now , (ptr - 1)); // Copy a certain amount of now into temp1 temp2[0] = ' '; // Make the first spot in temp2 = a space for (int x = 0 ; x < strlen(word2) ; x++) // Go through loop as many times as word { temp2[x + 1] = word2[x]; // Copy word into temp2 } strcat (temp1 , temp2); // Stick temp2 onto temp1 temp2[0] = ' '; // Make the first space in temp2 = space while (now[y] != NULL) // Loop while the i/o is still reading the line { temp2[y + 1] = now[(ptr + strlen(word1)) + y]; // Take the word out y++; } strcat (temp1 , temp2); // Stick temp2 onto temp1 again strcpy (now , temp1); // Copy the final result of temp1 to now } /********************************************************************************************** ********************************** FIND FUNCTION ******************************************** **********************************************************************************************/ void find(char now[] , int &ptr) // Definition for find { char word[21]; cout << "You have chosen to find a word\n" << "What word do you want to find in the line?\n"; cin >> word; for (int i = 0 ; i <= 80 ; i++) // It will loop 80 times { if (strncmp((now + i) , word , strlen(word)) == 0) ptr = i; // If it finds the word put a pointer there } } /********************************************************************************************** ********************************* INSERT FUNCTION ******************************************* **********************************************************************************************/ void insert(char now[] , int &ptr) // Definition for insert { char yn , word[21] , temp1[81] , temp2[81]; int i , y = 0; cout << "You have chosen to insert a word\n" << "Would you like to use your last found word for the place?(y/n)"; cin >> yn; yn = (char) toupper(yn); // Changes the letter to uppercase if (yn == 'Y') // If the user chooses yes { cout << "What word do you want to stick in?\n"; cin >> word; strncpy (temp1 , now , ptr); // Copy as much of now as we want into temp1 temp2[0] = ' '; // Had to do this to put spaces in sentence for (int x = 0 ; x < strlen(word) ; x++) { temp2[x + 1] = word[x]; // Loops through word copying it to temp2 } strncat (temp1 , temp2 , (x + 1) ); // Stick temp2's string ont the end of temp1 temp2[0] = ' '; // Once again to keep the space in the sentence while (now[ptr + y] != NULL) // So we don't finger out of the array { temp2[y + 1] = now[ptr + y]; // Grabs the rest of the sentence and puts it on y++; // Increments y } strncat (temp1 , temp2 , (y + 1)); // Stick temp2 onto the end of temp1 strcpy (now , temp1); // Copy temp1's string into now } else if (yn == 'N') // If the user does not use the found ptr { cout << "What position do you want to stick the word in?\n"; cin >> i; cout << "What word do you want to stick in?\n"; cin >> word; strncpy (temp1 , now , (i + 1)); // Copy the first part of the sentence over temp2[0] = ' '; // To provide room for spaces for (int x = 0 ; x < strlen(word) ; x++) { temp2[x + 1] = word[x]; // Loops through the word and puts it in temp2 } strncat (temp1 , temp2 , (x + 1)); // Sticks temp2 onto the end of temp1 temp2[0] = ' '; // Make sure we have spaces while (now[y + i] != NULL) // Loop and stay in array { temp2[y + 1] = now[i + y]; // Slowly copy the rest of now into temp2 y++; // Increment y } strncat (temp1 , temp2 , (y + 1)); // Copy the word we just made into temp1 strcpy (now , temp1); // Copy all of the string in temp1 into now } else // If the chose other than y or n cout << "Sorry your choice was invalad\n"; } /********************************************************************************************** ********************************** ERASE FUNCTION ******************************************* **********************************************************************************************/ void erase(char now[] , int &ptr) // Definition for erase { char yn , temp1[81] , temp2[81]; // Initialize int i , y = 0 , p; cout << "You have chosen to delete a word\n" << "Would you like to use your last found word for the place?(y/n)"; cin >> yn; yn = (char) toupper(yn); // Changes the letter to uppercase if (yn == 'Y') // If the user chooses yes { cout << "How many places do you want to delete?\n"; cin >> p; strncpy (temp1 , now , ptr); // Copy everything up to the ptr in now to temp1 temp2[0] = ' '; // So we have a space while (now[y] != NULL) // Loop while the array is valid { temp2[y + 1] = now[(ptr + p) + y];// Slowly copy now into temp2 (skiping the word) y++; // Increment y } strcat (temp1 , temp2); // Stick temp2 onto the end of temp1 strcpy (now , temp1); // Copy all of temp1 into now } else if (yn == 'N') // If the user does not want to use the ptr { cout << "What position do you want to start deleting at?\n"; cin >> i; cout << "How many spaces do you want to delete?\n"; cin >> p; strncpy (temp1 , now , i); // Copy now up to i into temp1 temp2[0] = ' '; // Get that space before we go in while (now[(i + p) + y] != NULL) // Loop while the arrays are valid { temp2[y + 1] = now[(i + p) + y]; // Copy the rest of the sentence in now to temp2 y++; // Increment y } strcat (temp1 , temp2); // Stick temp2 onto the end of temp1 strcpy (now , temp1); // Copy temp1 into now } else // If the user did not chose y or n cout << "Sorry your choice was invalad\n"; } /********************************************************************************************** ********************************** PRINT FUNCTION ******************************************* **********************************************************************************************/ void print(char original[] , char now[]) // Definition for print { ofstream qout("result.edi" , ios::out); // Make the out file stream (didn't work in main) cout << "Your original string was \n( " << original << " )\n" << "And your string now is \n( " << now << " )\n" << "Now check the current directory for the file result.edi" << endl; qout << "Your original string was \n( " << original << " )\n" << "And your string now is \n( " << now << " )\n"; } // Print it all out to screen and file /********************************************************************************************** *************************************** OUTPUT ********************************************** **********************************************************************************************/ /* This program will allow you to grab a line from a file and edit that line a number of different ways. you can input what you want the input file name to be Would you be interested in continuing?(y/n) y what would you like the input file to be (include the extension) ? line.dat The original string reads ( Unable to move the sands of time, I cry blood from these eyes. ). +--------------------------------------------------------------+ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦___________________+----------------------+___________________¦ ¦___________________¦ Make a choice ¦___________________¦ ¦___________________¦ ¦___________________¦ ¦___________________¦ R = Replace ¦___________________¦ ¦___________________¦ F = Find ¦___________________¦ ¦___________________¦ I = Insert ¦___________________¦ ¦___________________¦ D = Delete ¦___________________¦ ¦___________________¦ P = Print ¦___________________¦ ¦___________________+----------------------+___________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ +--------------------------------------------------------------+ r What word do you want to replace? sands What do you want to replace sands with? rockes +--------------------------------------------------------------+ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦___________________+----------------------+___________________¦ ¦___________________¦ Make a choice ¦___________________¦ ¦___________________¦ ¦___________________¦ ¦___________________¦ R = Replace ¦___________________¦ ¦___________________¦ F = Find ¦___________________¦ ¦___________________¦ I = Insert ¦___________________¦ ¦___________________¦ D = Delete ¦___________________¦ ¦___________________¦ P = Print ¦___________________¦ ¦___________________+----------------------+___________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ +--------------------------------------------------------------+ f You have chosen to find a word What word do you want to find in the line? blood +--------------------------------------------------------------+ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦___________________+----------------------+___________________¦ ¦___________________¦ Make a choice ¦___________________¦ ¦___________________¦ ¦___________________¦ ¦___________________¦ R = Replace ¦___________________¦ ¦___________________¦ F = Find ¦___________________¦ ¦___________________¦ I = Insert ¦___________________¦ ¦___________________¦ D = Delete ¦___________________¦ ¦___________________¦ P = Print ¦___________________¦ ¦___________________+----------------------+___________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ +--------------------------------------------------------------+ d You have chosen to delete a word Would you like to use your last found word for the place?(y/n)y How many places do you want to delete? 5 +--------------------------------------------------------------+ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦___________________+----------------------+___________________¦ ¦___________________¦ Make a choice ¦___________________¦ ¦___________________¦ ¦___________________¦ ¦___________________¦ R = Replace ¦___________________¦ ¦___________________¦ F = Find ¦___________________¦ ¦___________________¦ I = Insert ¦___________________¦ ¦___________________¦ D = Delete ¦___________________¦ ¦___________________¦ P = Print ¦___________________¦ ¦___________________+----------------------+___________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ +--------------------------------------------------------------+ i You have chosen to insert a word Would you like to use your last found word for the place?(y/n)y What word do you want to stick in? water +--------------------------------------------------------------+ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦___________________+----------------------+___________________¦ ¦___________________¦ Make a choice ¦___________________¦ ¦___________________¦ ¦___________________¦ ¦___________________¦ R = Replace ¦___________________¦ ¦___________________¦ F = Find ¦___________________¦ ¦___________________¦ I = Insert ¦___________________¦ ¦___________________¦ D = Delete ¦___________________¦ ¦___________________¦ P = Print ¦___________________¦ ¦___________________+----------------------+___________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ +--------------------------------------------------------------+ d You have chosen to delete a word Would you like to use your last found word for the place?(y/n)n What position do you want to start deleting at? 6 How many spaces do you want to delete? 2 +--------------------------------------------------------------+ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦___________________+----------------------+___________________¦ ¦___________________¦ Make a choice ¦___________________¦ ¦___________________¦ ¦___________________¦ ¦___________________¦ R = Replace ¦___________________¦ ¦___________________¦ F = Find ¦___________________¦ ¦___________________¦ I = Insert ¦___________________¦ ¦___________________¦ D = Delete ¦___________________¦ ¦___________________¦ P = Print ¦___________________¦ ¦___________________+----------------------+___________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ +--------------------------------------------------------------+ i You have chosen to insert a word Would you like to use your last found word for the place?(y/n)n What position do you want to stick the word in? 6 What word do you want to stick in? two +--------------------------------------------------------------+ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦___________________+----------------------+___________________¦ ¦___________________¦ Make a choice ¦___________________¦ ¦___________________¦ ¦___________________¦ ¦___________________¦ R = Replace ¦___________________¦ ¦___________________¦ F = Find ¦___________________¦ ¦___________________¦ I = Insert ¦___________________¦ ¦___________________¦ D = Delete ¦___________________¦ ¦___________________¦ P = Print ¦___________________¦ ¦___________________+----------------------+___________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ ¦______________________________________________________________¦ +--------------------------------------------------------------+ p Your original string was ( Unable to move the sands of time, I cry blood from these eyes. ) And your string now is ( Unable two move the rockes of time, I cry water from these eyes. ) Now check the current directory for the file result.edi RESULT.EDI Your original string was ( Unable to move the sands of time, I cry blood from these eyes. ) And your string now is ( Unable two move the rockes of time, I cry water from these eyes. ) */