Finding and replacing duplicates in string

Pages: 12
Before anything else, I seriously suggest properly indenting your code, because I think it keeps causing you confusion.
Where does your for loop (the one that starts on line 17) end? (PS: Your code doesn't compile.)

Again, I don't think you need your stringstream, it seems to only be causing you errors.
On line 13, you create 'in' when your data string is blank.
So line 22 never loops.
Last edited on
@OP

I think we are at the (often encountered) stage of the proceedings where it would be a good idea to post a copy of the question being asked in your assignment.

Another good move would be to apply some planning to this exercise and write down some simple pseudocode to what is going on here.

A twenty-questions style of providing help, and everybody is keen to do it, is not the best way of going about this.

Please post a copy of the assignment question in its entirety :)
Dead-horse-flogging time perhaps. But anyway ...

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <map>
#include <string>

int main()
{
    std::map<std::string, int> word_map;
    std::map<std::string,int>::iterator iter;
    
    std::cout << "Please enter number of words: ";
    int no_words{0};
    std::cin >> no_words;
    
    std::string input;
    for(int i = 0; i < no_words; i++)
    {
        std::cout << "Enter a fruit: ";
        std::cin >> input;
        
        iter = word_map.find( input );
        if ( iter != word_map.end() )
        {
            iter->second++;
            std::cout
            << input << " already encountered "
            << iter->second << " times\n";
        }
        else
            word_map.insert({input,0});
    }
    
    // MANGLE REPEATED WORDS
    int count{0};
    std::string word;
    for(auto it:word_map)
    {
        count = it.second;
        if(count > 0)
        {
            it.second = 0; // RESET BASE WORD COUNT
            for(int i = 0; i < count; i++)
            {
                word = it.first + std::to_string(i + 1);
                word_map.insert({word, -1}); // -1 TO PREVENT RE-USE
            }
        }
    }
    
    // DISPLAY UNIQUE WORDS
    std::cout << "The unique words are:\n";
    int unique_count{0};
    for(auto it:word_map)
    {
        if( it.second >= 0 )
        {
            std::cout << it.first << '\n';
            unique_count ++;
        }
    }
    std::cout << "No. of unique words: " << unique_count << '\n';
    
    // DISPLAY MANGLED WORDS
    std::cout << "The mangled words are:\n";
    for(auto it:word_map)
    {
        if(it.second == -1)
        {
            std::cout << it.first  << '\n';
        }
    }
    
    return 0;
}


Please enter number of words: 5
Enter a fruit: Banana
Enter a fruit: Orange
Enter a fruit: Apple
Enter a fruit: Banana
Banana already encountered 1 times
Enter a fruit: Orange
Orange already encountered 1 times
The unique words are:
Apple
Banana
Orange
No. of unique words: 3
The mangled words are:
Banana1
Orange1
Program ended with exit code: 0
Enter fruits should only appear once like
Enter number of fruits: 3
Enter fruits:
Mango
Apple
Oranbe
My bad. I assumed that you are not a copy-paste-programmer-sans-comprehension.

You had:
1
2
3
  vector <string> sentence;
  string input;
  while ( cin >> input ) { // read from istream "std::cin" 

I don't want to (re)type the test data, so I read the data from a different istream:
1
2
3
4
5
6
  const string data = "Banana Mango Apple2 Apple Orange Apple Apple";
  std::istringstream in( data ); 
  vector<string> sentence;
  string input;
  std::map<string, unsigned int> counter_of_each_word;
  while ( in >> input ) { // read from istream "in" 

If you want to read from cin, then do so.
1
2
3
4
5
6
  const string data = "Banana Mango Apple2 Apple Orange Apple Apple";
  std::istringstream in( data ); 
  vector<string> sentence;
  string input;
  std::map<string, unsigned int> counter_of_each_word;
  while ( cin >> input ) {

That loop does not have to run till the end of input:
while ( no_words < sentence.size() && cin >> input ) {
Thanks
Sir please I tried my level best but the outcome keeps coming 1
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
#include<iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
using namespace std;
int main(){
  using std::string; using std::vector; using std::cout;
  vector<string> sentence;
  int N;
  string input;
  cout <<"Enter number of words:\n";
  cin >> N;
  cout << "Enter fruits:\n";
  for(int i =0; i<N; i++)
    cin >> input;
  
  std::map<string, unsigned int> counter_of_each_word;
  
    auto count = ++counter_of_each_word[input];
    if ( 1 < count ) input += std::to_string( count );
    sentence.emplace_back(input);
  
  cout<< counter_of_each_word.size() << '\n';
  
  for ( auto w : sentence ) {
    cout << w << " ";
    cout << '\n';
  }

}
1
2
  for(int i =0; i<N; i++)
    cin >> input;

Go and look at the previous examples, and note CAREFULLY where the braces are - those { } things.

This isn't Python, where indentation guides scope.
I literally can't figure out how
Though I've tried many times
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
#include<iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
using namespace std;
int main(){
  using std::string; using std::vector; using std::cout;
  vector<string> sentence;
  int N;
  
  cout <<"Enter number of words:\n";
  cin >> N;
  
  
    
    string input;
    for(int i =0; i<N; i++)
    {
      cout << "Enter fruits:"<<'\n';
      cin >> input;
    
  std::map<string, unsigned int> counter_of_each_word;
  
    auto count = ++counter_of_each_word[input];
    if ( 1 < count ) input += std::to_string( count );
    sentence.emplace_back(input);
  
  cout<< counter_of_each_word.size() << '\n';
  
  for ( auto w : sentence ) {
    cout << w << " ";
    cout << '\n';
  }}

}
Last edited on
Anyone?
> std::map<string, unsigned int> counter_of_each_word;
Again, review the previous posts.

You decided to create a whole fresh new variable for every word you input.

In other posts, it was
1
2
3
4
5
  std::map<string, unsigned int> counter_of_each_word;
  for(int i =0; i<N; i++)
    {
      cout << "Enter fruits:"<<'\n';
      cin >> input;


Man i suggest you explain with comments in the done code please
Your indentation (at least as I see your posts) is not systematic. When it is not systematic, it is hard to see scopes. againtry did mention Python, where systematic indentation is mandatory.

Your code:
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
#include<iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
using namespace std;
int main() {
  vector<string> sentence;
  int N;
  cout <<"Enter number of words:\n";
  cin >> N;
  string input;
  for ( int i =0; i<N; i++ ) {
      cout << "Enter fruits:"<<'\n';
      cin >> input;
      std::map<string, unsigned int> counter_of_each_word;
      auto count = ++counter_of_each_word[input];
      if ( 1 < count ) input += std::to_string( count );
      sentence.emplace_back(input);
      cout<< counter_of_each_word.size() << '\n';
      for ( auto w : sentence ) {
          cout << w << " ";
          cout << '\n';
      }
  }
}

Does this formatting help you see what is in the body of the for loop?
We could write a function and move code into it to make it more clear:
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
#include<iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
using namespace std;

void body( vector<string>& sentence, string& input ) {
      cout << "Enter fruits:"<<'\n';
      cin >> input;
      std::map<string, unsigned int> counter_of_each_word;
      auto count = ++counter_of_each_word[input];
      if ( 1 < count ) input += std::to_string( count );
      sentence.emplace_back(input);
      cout<< counter_of_each_word.size() << '\n';
      for ( auto w : sentence ) {
          cout << w << " ";
          cout << '\n';
      }
}

int main() {
  vector<string> sentence;
  int N;
  cout <<"Enter number of words:\n";
  cin >> N;
  string input;
  for ( int i =0; i<N; i++ ) {
      body( sentence, word );
  }
}

Do you now see how you create and destroy counter_of_each_word inside one iteration of the loop? Only the sentence is common to all iterations.
I think it's input not word here
And also this code doesn't perform what I want

1
2
3
For ( int=0; I<N; I++){
        body ( sentence, input);
}
@keskiverto

againtry did mention Python, where systematic indentation is mandatory.

Small point but credit for that goes to salem c :)
All I'm asking is correction on my code but all I get people arguing about something and it's really confusing sorry for my language I don't English that much.
Also I'm just new to this and I learn through example of other people codes
Seems there's no help here
Anyways thanks
Finally i kinda managed to do the trick but I get errors I don't understand

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
40
41

#include <iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{ int no_of_names;
  string name;
  cout<< "Enter number of names";
  cin>> no_of_names;
  string nameslist[no_of_names];
  for (int i=0; i< no_of_names; i++)
  {
    cout << "Enter names:";
    cin >> name;
    nameslist[i]=name;
  }
 
  for (int i = 0; i<nameslist.length; i++)
  {
    for (int j =i +1; i< nameslist.length; j++)
    {
      int count=2;
      if (nameslist[i]=nameslist[j]+count)
      count++;
    }
  }
   string uniquenames[no_of_names];
   nameslist[o]= name;
   for (int k=0; k<nameslist.length; k++)
     {
       if (! uniquenames.contains(nameslist[k]));
       uniquenames[k]=nameslist[k];
     }
  
  cout << uniquenames.length;
  for(int x=0; x <nameslist.length; x++)
  {
    cout << nameslist[o];
  }
Last edited on
nameslist is an array, it does not have a a "nameslist.length". Use no_of_names instead.

Line 24: What are you trying to do here?
1. You're trying to add nameslist[j], a string, with count, an int. Addition between a string and an int is not defined.
2. You're also using =, which is assignment. I assume you are trying to compare two things, so == should be used. But again, I'm not sure what you're trying to compare.
Did you mean to simply do if (nameslist[i] == nameslist[j])?

Arrays with non-compile-time lengths are not allowed in standard C++, although GCC has an extension for them. Use a vector or new[]/delete[].
Last edited on
Topic archived. No new replies allowed.
Pages: 12