how to compare words in c++

i am asked to remove duplicated words from a text and write to a new file in c++.

I am some ideas but just don't konw how to write it out.
I want to compare each word and replaced the duplicated word with it's single occurrence. Any help?

for example:
the the sun sun is bright

and change the text to:
the sun is bright
i am asked to remove duplicated words from a text and write to a new file in c++.

I am some ideas but just don't konw how to write it out.
I want to compare each word and replaced the duplicated word with it's single occurrence. Any help?

for example:
the the sun sun is bright

and change the text to:
the sun is bright 

You need to read the string with stringstream, then store all the words in a std::set to filter out duplicate words. Then you print out all words in the container.

Do you know how to do it?
I dunno how to do it. please help
Have you known these headers?
1
2
#include <sstream>
#include <set> 
i am new to c++. We are asked to use <iostream>, <iomanip>, <cstring>, <fstream> only.
I want to compare the latter and former word, but how? any suggestion?
You need to store two strings at the same time. Read one word at a time from the file and compare it with the previous word. If they are different, write out the previous word to the new file. Then the current word replaces the previous word and you read the next word from the input. After reading all the words, if the final word is different to the previous, write that out too.

Because you are limited to <cstring>, that means using C-style character arrays rather than C++ strings.

You might need to use strcmp(), strcpy() at least.
http://www.cplusplus.com/reference/cstring/strcpy/
http://www.cplusplus.com/reference/cstring/strcmp/

To read all words from a file
1
2
3
4
5
6
    std::ifstream fin("filename.txt");
    char word[50];
    while (fin >> word)
    {
        std::cout << word << ' ';
    }
Topic archived. No new replies allowed.