delete duplicated words with at most 20 characters

I would like to write a programme to remove the duplicate words with at most 20 characters in a passage inside a txt file with at most 1000 words and the no. of duplication has no limit and is unknown. Please help !!!! The libraries that can be used are <iostream> <iomanip> <cstring <fstream> only !!! Please Help!! Really appreciate !!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
using namespace std;

int main() {
	char words[1000][20];
	ifstream fin;
	fin.open("Input.txt"); // open file for input
	if (fin.fail()) { // if fail to open
		cout << "Unable to open file Input.txt " << endl;
		return -10;
	}
	//read all content
	cout << " Input document:" << endl;
		if (!fin.fail()) {
			fin >> words [1000];
			cout << words << endl;
		}
		else return -11;
	ofstream fout; // output the new file
	fout.open("Output.txt"); // creat the file for output
	if (fout.fail()) {
		cout << "Unable to open file Output.txt" << endl;
		return -10;
	}
	//correcetion 1
	for (int i = 0; i < 1000; i++) {
	if (words[i][20] == words[i + 1][20])
		{
			words[i + 1][20] = NULL;
	}
		fout << words[i];
	}
	cout << words[1000];
	fin.close();
    fout.close();
	
	return 0;
}}
Last edited on
You can remove all duplicate words of any size, not just upto 20 characters and print to console with ... :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    ifstream file("F:\\test.txt");
    string words;
    vector<string> wordlist;
    int counter{};
    if(file)
    {
        while (file>>words)
        {
            if(find(wordlist.begin(), wordlist.end(), words) == wordlist.end())
            {
                wordlist.push_back(move(words));
            }
                else
            {
                counter++;
            }
        }
    }
    else
    {
        cerr<<"Could not open file \n";
    }

    cout<<"Number of duplicate words removed: "<<counter<<"\n";
    for(auto& elem : wordlist)
    {
        cout<<elem<<" ";
    }
}
Sorry that i can only use the library of iostream, fstream , cstring, iomanip,cmath. Any way help ???? PLease !!!!!
Topic archived. No new replies allowed.