In: Computer Science
In c++ please. Write a program that opens a specified text tile then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
C++ code :
#include <bits/stdc++.h>
using namespace std;
// driver code
int main()
{
// filestream variable file
fstream file;
string word, t, q, filename;
set<string> s;
//set<string>::iterator it;
// filename of the file
filename = "paragraph.txt";
// opening file
file.open(filename.c_str());
// extracting words from the file
while (file >> word)
{
// displaying content
cout << word <<
endl;
s.insert(word);
//insert words into set.
}
for(auto i=s.begin();i!=s.end();i++)
{
cout<<*i<<endl;
//print the words.
}
return 0;
}
Output: