Write in Java Two operations often performed on text files are a simple \'find\'
ID: 652750 • Letter: W
Question
Write in Java
Two operations often performed on text files are a simple 'find' operation and a 'find/replace'.
Description:
Create a class Sed (simple editor) that first asks the user if they want the program to act in 'find' mode or 'find/replace' mode.
In find/replace mode: the program should ask the user for 3 items:
A filename
A word to find
A word to replace the word that was found
The program should read in the file found at 'filename' and replace every instance of the 'find' word with the 'replace' word. The program should then print any output to the file a.out
In find mode: the program instead should ask the user for two items:
A filename
A word to find
The program should read in the file found at 'filename' and find every instance of the 'find' word. Then, printing to the console, the program should print every line that contains the 'find' word along with it's line number.
In either case, you can assume the whitespace in the file only consists of single spaces (' ') or single newline characters (' ').
Example 1 (Find Mode):
Input: lorem_ipsum.txt
Example 2 (Find/Replace)
Input: lorem_ipsum.txt
Output: a.out
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Sed {
private:
ifstream myfile;
public:
Sed() {
int choice;
cout << "Select a mode: ";
cout << "1. Find/Replace ";
cout << "2. Find ";
cout << "3. Exit ";
cin>>choice;
switch(choice)
{
case 1:
findReplace();
break;
case 2:
find();
break;
default:
return;
}
}
void find() {
string line;
char inputFile[20];
char toFind[20];
/*take input from user */
cout << "Enter the name of a file: ";
cin>>inputFile;
cout << " ";
cout << "Enter a String to find: ";
cin>>toFind;
cout << " ";
myfile.open(inputFile); //open the file
if (myfile.is_open()) {
int i = 0;
while (getline(myfile, line)) { //read each line by getline function
++i; //tracks the line number
size_t found = line.find(toFind); //find if the user required data is found or not
if (found != std::string::npos) {
cout << i << " " << line << " "; //displays if the data is found in the line
}
}
myfile.close(); //file close
} else {
cout << " Unable to open file !! ";
}
}
void findReplace() {
string line;
char inputFile[20];
char toReplace[20] ;
char toFind[20];
cout << "Enter the name of a file: ";
cin>>inputFile;
cout << " ";
cout << "Enter a String to find: ";
cin>>toFind;
cout << " ";
cout << "Enter a String to replace: ";
cin>>toReplace;
cout << " ";
myfile.open(inputFile);
if (myfile.is_open()) {//read each line by getline function
int i = 0;
while (getline(myfile, line)) {
++i;
size_t found = line.find(toFind);//find if the user required data is found or not
if (found != std::string::npos) {
/* repalaces the found word by the user given word*/
line.replace(found, (sizeof (toFind) / sizeof (char)) - 1, toReplace);
cout << line << " "; //displays the line
}
}
myfile.close();//file close
} else {
cout << " Unable to open file !! ";
}
}
};
int main() {
Sed obj; //calls the constructor of the class Sed object obj
return 0;
}
-------------------------------------------------------
OUTPUT
Select a mode:
1. Find/Replace
2. Find
3. Exit
2
Enter the name of a file: input.txt
Enter a String to find: Donec
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam
3 viverra nec consectetur ante hendrerit. Donec et mollis dolor.
6 Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum
11 Ut convallis libero in urna ultrices accumsan. Donec sed odio eros.
12 Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.