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

Thank you so much Write a program that shall eliminate duplicate words from a te

ID: 3877674 • Letter: T

Question

Thank you so much Write a program that shall eliminate duplicate words from a text file. The program 1. Ask the user to enter a name of an existing text file; if the file does not exist or cannot be opened uplicate words, except for the first occurrence. For shall: for any other reason, the program shall print an error message and terminate. the purpose of this exercise, a word is any sequence of characters that does not contain any spaces, and two words are duplicates if they differ at most in the character case (e.g., "Mary" and "mArY" are duplicates) Write the text without duplicates into the file nodups-XXX, where XXX is the name of the original file. If the output file cannot be created, the program shall print an error message and terminate. 3. 4. Test program by removing duplicates from Othello http://www.gutenberg.org/cache/epub/2267/pg2267-images.html

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>

using namespace std;

int main(){

   
     int found;
     string filename;
     string line;
     cout << "Enter the file name:";
     cin >> filename;
     ifstream fin(filename.c_str());
     if (!fin){
          cout << "Error opening file ";
          return 0;
     }
     vector<string> list;
     string word;
     //string out = "nodups-" + filename;
     //string out = "nodups-" + filename;
     //ofstream fout(out.c_str());

     while(!fin.eof()){
       
         //line ="";
         getline(fin,line);
         /*
         if (line.size() == 0){
          
            list.push_back(" ");
            continue;
         }
         */
         stringstream ss(line);
         while (ss >> word){
          
            found = 0;
            for (int i = 0; i<list.size(); i++){
                if (list[i].length() == word.length()){
                   found = 1;
                   for (int j = 0; j<list[i].length(); j++){
                       if (tolower(list[i][j]) != tolower(word[j])){
                          found = 0;
                          break;
                       }
                   }
                   if (found == 1)
                      break;
                }
            }
            if (found == 0){
               list.push_back(word);
            }
         }
         list.push_back(" ");
     }
     fin.close();
   
     //cout << list.size() << endl;
    
     string out = "nodups-" + filename;
     ofstream fout(out.c_str());
     for (int i = 0; i<list.size(); i++){
         fout << list[i] << " ";
     }
     fout.close();
    
     return 0;
}

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