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

Write a C++ Program that uses: Strings Passing Strings to functions Files Input

ID: 3778946 • Letter: W

Question

Write a C++ Program that uses: Strings Passing Strings to functions Files Input and output.

Project Requirements: Write a function called removeRepeats that, given a string, removes any repeated letters and returns a new string.

Write a main method that reads words (strings) from a file and writes the new strings (no repeated letters) to another file.

Several options are left for the programmer to determine: Use of either c-string(char arrays) or thestring class. Use of a container (array or vector) to store the strings. You can either store the strings in an array or vector - or just read and write without storage.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
string removeRepeats (string s){
string newstr;
  
for(int i=0; i<s.length(); i++){
bool flag = false;
for(int j=0; j<newstr.length(); j++){
if(newstr[j] == s[i]){
flag = true;
break;
}
}
if(!flag){
newstr = newstr + s[i];
}
}
  
return newstr;
  
}
int main() {

ifstream inputFile;
inputFile.open("input.txt");
ofstream outputFile;
outputFile.open ("output.txt");
string s;
if (inputFile.is_open()) {
while (!inputFile.eof()) {


inputFile >> s;
string newstr = removeRepeats(s);
outputFile << newstr<<" ";

}
}
inputFile.close();
outputFile.close();
cout<<"File has been generated"<<endl;
return 0;
}

output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                    

sh-4.2$ main                                                                                                                                                                                                                                            

File has been generated

input.txt

This is suresh murapaka. Welcome to c++ programming language.

output.txt

This is sureh murapk. Welcom to c+ progamin langue.

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