I'm making a word unscrambler, an anagram solver of sorts. So, I've stored all possible variations of the word in storedstrings which is an array of strings. Now, i need to delete all the same strings in storedstrings and then store the unique strings in another array of strings. This is my function and it isn't working. Dunno why because it appears correct. Some help please?

//function to delete same strings from storedstrings
void del_duplicate()
{
    int i,lim,k,n=0;
    lim=factorial(len);
    for(i=0;i<lim;i++)
    {
        for(k=0;k<lim;k++)
        {
            if(i!=k)
            {
                if(!(storedstrings[i].compare(storedstrings[k])))
                {
                    storedstrings[k].clear();
                }
            }
        }
    }
    for(i=0;i<lim;i++)
    {
        if(!(storedstrings[i].empty()))
            newstoredstrings[n++].assign(storedstrings[i]);
        cout<<n<<" "<<newstoredstrings[n-1];

    }
    total=n;
}

If you sort the array of strings ...

you can then traverse the sorted array

and skip over all duplicates

I print the strings newstoredstring in another function. But nothing gets printed. For instance if i write this,

   for(i=0;i<lim;i++)
    {
        cout<<i+1<<". "<<storedstrings[i]<<endl;
    }

it doesn't print anything at all. But if i write this,

    for(i=0;i<lim;i++)
    {
        cout<<endl;
        for(k=0;k<len;k++)
            cout<<storedstrings[i][k];
    }

the strings get printed. So, what's happening is that though the strings are stored in storedstrings, the condition !storedstrings[i].empty() is not satisfied. So, the entire function fails to work.

Edited 2 Years Ago by messr135

You will need to provide more information about your program ... for example ... provide a complete working program that illustrates your question/problem?

Note:

storedstrings[i][k]
and

storedstrings[i]

are NOT the same thing !

But without your code that defines 'storedstrings' ... we can not really say much more.

Edited 2 Years Ago by David W

This being C++, you might consider using a container that does deduplication for you. A std::set comes to mind. If you use a std::set of strings to store your strings in, instead of an array of strings, when you're done adding your strings to it, there are no duplicates.

As well advised by Dani's @Moschops above ...

you can streamline your code and logic flow by using the STL 'set' container ...

If you want to preserve the original order of the elements,
that could also be handled using sets by using something like this ...

// eraseDuplicateStoredStrings.cpp //


/**
    I'm making a word unscrambler, an anagram solver of sorts.
    So, I've stored all possible variations of the word instoredstrings
    which is an array of strings.
    Now, i need to delete all the same strings in storedstrings
    and then store the unique strings in another array of strings ...
**/

#include <iostream>
#include <string>
#include <vector>
#include <set>


using namespace std;

class MyStrings
{
public:
    // default ctor...
    MyStrings() {}
    // ctor from array ...
    MyStrings( const string ary[], size_t size ) ;

    void del_duplicates();

private:
    vector< string > storedstrings;
    friend ostream& operator << ( ostream& os, const MyStrings& ms );
} ;


// friend def'n ...
ostream& operator << ( ostream& os, const MyStrings& ms )
{
    vector< string >::const_iterator it;
    for( it = ms.storedstrings.begin();
         it != ms.storedstrings.end();
         ++ it )
    {
        os << *it << ' ';
    }
    return os;
}

// ctor def'n ...
MyStrings::MyStrings( const string ary[], size_t size )
{
    vector< string > tmp( ary, ary+size );
    storedstrings.swap( tmp );
}

void MyStrings::del_duplicates()
{
    // 1st step ... rid duplicates by getting (unique) 'set version'
    set< string > tmpSet;
    vector< string >::const_iterator cv_it;
    for( cv_it = storedstrings.begin(); cv_it != storedstrings.end(); ++ cv_it )
    {
        tmpSet.insert( *cv_it );
    }

    // 2nd step ... get tmp vector copy of set
    // elements in same order as order in original vector
    vector< string > tmpVec;
    tmpVec.reserve( tmpSet.size() );
    set< string >::iterator s_it;
    for( cv_it = storedstrings.begin(); cv_it != storedstrings.end(); ++ cv_it )
    {
        s_it = tmpSet.find( *cv_it );
        if( s_it != tmpSet.end() ) // found
        {
            tmpVec.push_back( *cv_it );
            tmpSet.erase( s_it ); // Ok ... now, no more
                                  // copying of this element //
        }
    }

    // last step ...
    storedstrings.swap( tmpVec );
}


int main()
{
    const string myTestStrs[] = { "sam", "pam", "sam",
                                  "dan", "pam", "ann",
                                  "sam" };
    const size_t size = sizeof myTestStrs / sizeof(string) ;

    // construct from array ... calls ctor for this ...
    MyStrings myStrs ( myTestStrs, size );

    cout << "With duplicates ... \n" << myStrs << endl;

    myStrs.del_duplicates();

    cout << "Now NO duplicates ... \n"<<  myStrs << endl;

    cout << "\n\nPress 'Enter' to continue/exit ... \n";
    cin.get();
}

Edited 2 Years Ago by David W

This article has been dead for over six months. Start a new discussion instead.