#include<iostream> #include<fstream> #include<stdlib.h> #include<string> using n
ID: 3886452 • Letter: #
Question
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string>
using namespace std;
void readFile(string inputFileName, string *words, int num){
ifstream fin;
char *p = &inputFileName[0];
fin.open(p);
for (int i = 0; i<num; i++){
getline(fin,words[i]);
}
fin.close();
}
void writeFile(string outputFileName, string *words, int num){
ofstream fout;
char *p;
p = &outputFileName[0];
fout.open(p);
for (int i = 0; i<num; i++){
fout << words[i] << endl;
}
fout.close();
}
int main(int argc, char **argv){
//Check correct number of arguments
if (argc != 5) {
cout << "Error" << endl;
return 0;
}
//Handling cmd line args
//YOU MUST INITYIALIZE THEIR VALUES
string inputFileName = argv[1];
string outputFileName = argv[2];
int numberOfWords = atoi(argv[3]);
char *lettersToReplace;
char *p;
int found;
//Checking valid input
if (numberOfWords <= 0) {
cout << "Error" << endl;
return 0;
}
string words[numberOfWords];
readFile(inputFileName, words,numberOfWords);
for (int i = 0; i<numberOfWords; i++){
p = argv[4];
found = 0;
for (int j = 0; *p != ''; j++){
if (words[i][0] == *p)
found = 1;
p++;
}
if (found == 1)
words[i] = "---";
}
writeFile(outputFileName, words,numberOfWords);
return 0;
}
Explanation / Answer
Hi, here is the replaceWord function, you need,
void replace(string &word,char toReplaceInword)
{
if(word[0]==toReplaceInword) //check if it starts with given char
word="----"; //set the word to 4 dashes
}
Thumbs up if this was helpful, otherwise let me know in comments.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.