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

Inputs: An input file named \"sentences\" that contains a sequence of sentences.

ID: 3598376 • Letter: I

Question

Inputs: An input file named "sentences" that contains a sequence of sentences. The number of sentences as well as the length of each sentence may be arbitrarily long. There are no whitespace constraints - for example, two sentences may have multiple spaces between them, a sentence may or may not start on its own line, etc. However, you may assume that the characters in the sentences are all ASCII (so, no need to handle special Unicode characters).

An input file named "words" that contains a sequence of words, one per line. Words may be of arbitrary length but a precondition to main is that there are between 2 and 8 words in this file. inputs contain no non-printable ASCII characters (so you don't need to test for that).

How can i write a program that produces the following outputs:

An output file named "match" that contains every sentence with the first word in file "words". Each sentence should be on its own line. An output file named "replace" that contains the same data as above, but with every occurrence of the first word of "words" replaced with the second word of "words".

An output file named "allwords" that contains every sentence with each of the words in file "words" (not necessarily contiguous or in the same order). Each sentence should be on its own line and in the same order as in the sentences file.

Output messages to the screen in the following order (and using this exact text):: sentences that match the first word saved in file match. The longest sentence matching the first word has words, and the 2nd longest has words. sentences that match every word saved in file allwords. Note that you should handle the case where the two longest sentences are of the same length (unlike similar code we gave in class). If there are any errors (including cases of missing files, etc.), output appropriate messages to cerr. There are some additional rules that apply to all outputs: All matches are case-insensitive - for example, the word "ate" is considered to match sentence "I aTe pizza". Sentences in the output files should start in an upper case character (whether or not the input sentence did), but should preserve case otherwise. how can i code this without using anything beyond functions and strings

No Array and Vector using c++

Explanation / Answer

C++ code:

#include <iostream>

#include <string>

#include <vector>

#include <sstream>

#include <fstream>

using namespace std;

int main(){

// input sentences file

ifstream infile;

infile.open("sentences");

// input words file

ifstream infile_words;

infile_words.open("words");

// output match file

ofstream outfile_match;

outfile_match.open("match");

// output replace file

ofstream outfile_replace;

outfile_replace.open("replace");

// output allwords file

ofstream outfile_allwords;

outfile_allwords.open("allwords");

string sentence; // stores a line from input file

string word; //store a word from sentence

string first_word; // first word from sentence

string second_word; // second word from sentence

string toReplace; // stores the updated string to be saved in replace file

string all_words; // stores string containing all words in string

getline(infile_words, first_word); // extract first word from words file

getline(infile_words, second_word); // extract second word from words file

all_words = first_word+" "+second_word;

// append all words from words file to a single all_words string

while(getline(infile_words, word)){

all_words = all_words + " "+word;

}

// read line by line(sentence wise) from input file

while(getline(infile, sentence)){

// if first word of file words is present in sentence

if (sentence.find(first_word) != string::npos) {

// write sentence into match file

outfile_match<<sentence<<endl;

// now search all occurences of first word in sentence

// and replace that with second word of words file

size_t index = 0;

toReplace = sentence; // stores updated sentence

while(true) {

// Locate the substring to replace.

index = toReplace.find(first_word, index);

if (index == std::string::npos) break;

// Make the replacement

toReplace.replace(index, first_word.length(), second_word);

index += second_word.length();

}

// write updated sentence to replace file

outfile_replace<<toReplace<<endl;

}

// stringstream for current sentence

stringstream ss(sentence);

size_t index = 0;

int is_present = 1; // bit to store whether all words of sentence are present in words file or not

while(ss>>word) {

// if a word is found, then chek for next word

if (all_words.find(word) != string::npos){

continue;

}

// if a word is not found, set is_present bit to false and break

else{

is_present = 0;

break;

}

}

// if all words of sentence are present in words file

if(is_present == 1){

// write sentence to all_words file

outfile_allwords<<sentence<<endl;

}

}

return 0;

}