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?
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.
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.