Hey guys, I'm trying to make a program which will count the number of unique words in sting then finding the duplicates words and replace them to make them all unique.The first line represent the number of words which will be input
Example
Input. Output
6. 4(number of unique words)
Banana. Banana
Mango. Mango
Apple. Apple
Orange. Orange
Apple. Apple1
Apple. Apple2
The duplicates must be replaced with numbers in front to differentiate them
So far, I have only the program to find duplicates
I think the emplace function will give you the best result since it's return value will show if the word was already in the set. Please look it up because the return value is actually a pair...
You will still have trouble with punctuation marks and similar, but you can write a dropNonAlpha function pretty easily.
The code below almost, but not quite, does the deed.
The logic is to insert only unique values to the 'sentence'.
However, the way we check for uniqueness and make words unique has a flaw.
The map keeps track of (real) words and how often they are repeated. The rest follows by simply looping through the number of repetitions and adding the increment number to the string - one possibility of many.
Thanks but I think @keskiverto's programm is the one I'm looking for tho I just need to allow the program to accept user input and the first duplicate should have one in front of it and etc
#include <iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
usingnamespace std;
int main(){
using std::string; using std::vector; using std::cout;
string data;
int N;
cout << "Enter number of fruits to be entered:";
cin >> N;
std::istringstream in( data );
vector<string> sentence;
cout << "Enter words\n";
for(int i=0; i<N ;i++){
string data;
cin >> data;
std::map<string, unsignedint> counter_of_each_word;
while ( in >> data ) {
auto count = ++counter_of_each_word[data];
if ( 1 < count ) data += std::to_string( count );
sentence.emplace_back(data);
}
for ( auto w : sentence ) {
cout << w ;
cout <<'\n';
}
cout << '\n' << counter_of_each_word.size() ;
}