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

Description: In this homework, you will implement reading & writing to files and

ID: 3884696 • Letter: D

Question

Description: In this homework, you will implement reading & writing to files and passing by reference. Problem: You will be given a list of words in a file. You must then replace all words that start with any of the characters in a list (from command line args). Finally, you will output the changed list to a file. You will be reading your arguments from the command line. The name of the input file will be passed into your program as argument 1, argv[1]. The name of the output file will be passed into your program as argument 2, argv [2]. The number of words will be passed into your program as argument 3, argv [3]. The letters to replace will be passed into your program as argument 4, argv [4] You must create an array called words, then you must read the contents of this file into it. You must use the function: void readFile (string fileName, string words []) Then for every character in the list of characters, replace all words that start with the that character. These words will be replaced with four dashes, You must use the function: void replaceWord(string &word;, char toReplaceInWord) Then you must write the array words to file in. You must use the function: void writeFile (string fileName, string words[], int numberOfWords)

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;

void readFile(const char *ifn, char words[100][50], int now);

void writeFile(const char *ofn,char words[100][50],int now);

std::ifstream ifd;
std::ofstream ofd;

int tnl; // total no. of lines

int main(int argc, char *argv[])
{
   char ifn[128]; // input filename
   char ofn[128]; // output filename
   int now; // number of words
   char words[100][50];
   char srchletters[30];

   int i,j;

   if (argc!=5)
    {
       cout << "Error ";
       return 1;
    }
   now=atoi(argv[3]);
   if(now<=0)
    {
      cout << "Error ";
      return 2;
    }

   strcpy(ifn, argv[1]);
   strcpy(ofn, argv[2]);
   strcpy(srchletters, argv[4]);

   ifd.open(ifn);
   ofd.open(ofn);
    while(ifd)
    {
       readFile(ifn, words, now);
  
        for(j=0;srchletters[j]!='';j++)
           for(i=0;i<tnl;i++)
              if( words[i][0]==srchletters[j])
                 strcpy(words[i],"----");

       writeFile(ofn,words,tnl);

    }
}

void writeFile(const char *ofn,char words[100][50],int now)
{
   for(int i=0;i<now;i++)
    {
      ofd.write(words[i],strlen(words[i]));
      ofd.write(" ",1);
    }
}
void readFile(const char *ifn, char words[100][50], int now)
{
   tnl=0;
   if( ifd.is_open() )
       for(int i=0;i<now;i++,tnl++)
         ifd.getline(words[i],80);
}

/*

lenovo@lenovo-Vbox:~/chegg$ g++ -Wall hw3.cpp -o hw3
lenovo@lenovo-Vbox:~/chegg$

lenovo@lenovo-Vbox:~/chegg$ cat words.txt
Java
R
Bash
Lisp
Unix Shell
TeX
Index out of Bounds
Quality
Fortran
Go
Stack Overflow
Objective-C
Assembly
Visual Baisc
C++
Haskell
Python
Matlab
JavaScript
Erlang
Ruby
Groovy
Tickle
Clojure
Scala
Rust
Mathematica
lenovo@lenovo-Vbox:~/chegg$

lenovo@lenovo-Vbox:~/chegg$ ./hw3 words.txt rpwords.txt 20 JCBER
lenovo@lenovo-Vbox:~/chegg$

lenovo@lenovo-Vbox:~/chegg$ vi rpwords.txt
----
----
----
Lisp
Unix Shell
TeX
Index out of Bounds
Quality
Fortran
Go
Stack Overflow
Objective-C
Assembly
Visual Baisc
----
Haskell
Python
Matlab
----
----
----
Groovy
Tickle
----
Scala
----
Mathematica

lenovo@lenovo-Vbox:~/chegg$ ./hw3 words.txt rpwords1.txt 10 JCBER
lenovo@lenovo-Vbox:~/chegg$

lenovo@lenovo-Vbox:~/chegg$ vi rpwords1.txt
----
----
----
Lisp
Unix Shell
TeX
Index out of Bounds
Quality
Fortran
Go
Stack Overflow
Objective-C
Assembly
Visual Baisc
----
Haskell
Python
Matlab
----
----
----
Groovy
Tickle
----
Scala
----
Mathematica

*/