5
Contributors
8
Replies
16
Views
5 Years
Discussion Span
Last Post by mike_2000_17
Featured Replies
0

Duplicate words? Duplicate letters? Duplicity?

What have you done on this so far? Are you allowed to use methods from the standard library (e.g., std::sort, and std::unique)?

0

Duplicate words in file..

i guess sort n unique cannot be used for vectors.....

0

Can anyone please post the code using map or set ............. I tried doing it with vectors..

1

I tried doing it with vectors..

The point is that we would like to see what you've done so far so we can help you. The fact that you have not finished this indicates that you have specific errors. If you describe these errors with some relevant code snippets we can help you work through them.

Us flat out giving you code is not beneficial to your (or us, in the long run).

0
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>

using namespace std ;
int findDuplicate(string a,string b);

int main( )
{
    ifstream first( "C:\\abc.txt" ) ;
    //ofstream last( "C:\\abc1.txt" );
    vector<string> stringTable;
    string temp,temp1,temp2;
    int i,j=0,flag=0;
    //vector<string>::iterator it;



    while( !first.eof())
        {
            first >>temp ;
            stringTable.push_back( temp );
        }

    for (i=0; i<(int)stringTable.size();i++)
    {
        temp1=stringTable[i];
        //j=0;
        for (j=i+1; j<(int)stringTable.size(); j++)
        {
            temp2=stringTable[j];
            flag=findDuplicate(temp1,temp2);        
            if(flag==1)
            {
                stringTable[j]=" ";
                j++;
            }
            else
            j++;
            continue;           
        }
    }
    for (i=0; i<(int)stringTable.size();i++)
    {  
        cout << stringTable[i] << endl;  
    }

    /*for( i=0;i<(int)stringTable.size();i++)    
    {       
        last << stringTable[i];       
        last << " ";  
    }
    last.close(); */        
}


int findDuplicate(string a,string b)
{
    int i,j;

    for(i=0;a[i]!='\0';i++)
    {
            for(j=0;b[j]!='\0';j++)
            {
                    if(a[i] == b[j])
                    continue;
            }
    }

    if (i==j)
    {
            return 1;
    }
    else
            return 0;

}

Edited by mike_2000_17: Fixed formatting

0

**Please use code tags**

>>int findDuplicate(string a,string b)

In C++, strings can be compared with ==. There is no need for this function.

>>int i,j=0,flag=0;

Declare your variables where you first use them, whenever possible.

>>for (i=0; i<(int)stringTable.size();i++)

Prefer iterators to indices when doing a simple linear traversal:

for (vector<string>::iterator it = stringTable.begin(); it != stringTable.end(); ++it) {
  temp2 = *it; //you don't really need temp2, just use *it everywhere instead.

>>i guess sort n unique cannot be used for vectors.....

They certainly can, that's what they are for, mostly used with vector (or some other STL containers). Just see the example for std::unique.

Hint: Using a set instead of a vector is probably the easiest way to do this (although using "sort" and then "unique" is arguably more efficient). You can even write almost the entire program in one line (but that's going a bit too far).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.