Write a C++ Program that uses: Strings Passing Strings to functions Files Input
ID: 3804274 • Letter: W
Question
Write a C++ Program that uses:
Strings
Passing Strings to functions
Files Input and output.
Preparatory Reading:
Chapter 8 in Problems Solving with C++
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 (with no repeated letters, regardless of case,) 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.
As you can see, many of the implementation details are left up to the programmer. At this point in the course you should be able to make design decisions based on good programming practices.
Explanation / Answer
code:
#include <iostream>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include <fstream>
using namespace std;
string removeRepeat(string str)
{
map<char,int> hash;
string newstr="";
int i=0;
while(i<str.size())
{
if(hash.count(str[i])==0)
{
hash[str[i]]=1;
newstr+=str[i];
}
i++;
}
return newstr;
}
int main()
{
ifstream myfile ("input.txt");
ofstream fw;
fw.open ("output.txt");
string line="";
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
fw <<removeRepeat(line)<<" ";
}
myfile.close();
}
fw.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.