I have a homework for learning functions.
I have to get a string from user and first I must arrange it in alphabetic order and the I must remove the duplicates.
for finding it I used 2 loops that first one gets a letter and the second one will check for it in the rest of string
my problem is that I don't know that when I find the duplicate how should I remove it
or at least put it in another string without the duplicates
PS. I'm not allowed to touch the int main()
For removing duplicates from a string that is already sorted, loop through the characters of this string and add the character to a result string if it differs from its predecessor.
You could also let the STL do all the hard work for you:
Well, you probably ought still to have #include <string>
Quoting my previous post:
For removing duplicates from a string that is already sorted, loop through the characters of this string and add the character to a result string if it differs from its predecessor.
Once you have your own sorting routine, then you can replace sort() in the code below and then remove the #include <algorithm> .
You still need to consider what to do about upper and lower case. If they are distinct then fine as it is. If they are to be taken as the same, then this will affect all comparisons in the sorting and the equality test before adding to result.
#include <iostream>
#include <string>
#include <algorithm> // for sort(); you can replace with your own and remove this include
usingnamespace std;
string sortAndRemoveDuplicates( string str );
//==========================================================
int main()
{
string str;
cout << "Enter a string: ";
getline( cin, str );
cout << "Final string is: " << sortAndRemoveDuplicates( str ) << '\n';
}
//==========================================================
string sortAndRemoveDuplicates( string str )
{
if ( str.size() <= 1 ) return str; // trivial and unlikely case
sort( str.begin(), str.end() ); // replace with your own sorting routine
string result; result += str[0];
for ( int i = 1; i < str.size(); i++ )
{
if ( str[i] != str[i-1] ) result += str[i]; // As long as str is sorted, not equal to predecessor ...
} // ... means it is not a duplicate; so add to result
return result;
}
//==========================================================
Enter a string: remove duplicates from here
Final string is: acdefhilmoprstuv
If you want to remove duplicates without sorting first then here are a couple of options. (The latter has some dubious type conversions, and it's difficult to make it more portable without some extra #include<> statements.)
#include <iostream>
#include <string>
usingnamespace std;
string RemoveDuplicates1( string str );
string RemoveDuplicates2( string str );
//==========================================================
int main()
{
string str;
cout << "Enter a string: ";
getline( cin, str );
cout << "Final string is: " << RemoveDuplicates1( str ) << '\n';
cout << "Final string is: " << RemoveDuplicates2( str ) << '\n';
}
//==========================================================
string RemoveDuplicates1( string str ) // O(N^2) algorithm - actually not!
{
if ( str.size() <= 1 ) return str;
string result;
for ( int i = 0; i < str.size(); i++ )
{
bool seen = false;
for ( int j = 0; j < i; j++ ) // test all previous
{
if ( str[i] == str[j] )
{
seen = true;
break;
}
}
if ( !seen ) result += str[i]; // if not used previously then add to result
}
return result;
}
//==========================================================
string RemoveDuplicates2( string str ) // O(N) algorithm
{
if ( str.size() <= 1 ) return str;
constint MAXCHARS = 256; // WARNING: needs to be the size of your character set
bool seen[MAXCHARS] = { 0 }; // indicates occurrence of this character number
string result;
for ( int i = 0; i < str.size(); i++ )
{
char c = str[i];
if ( !seen[c] ) result += c;
seen[c] = true;
}
return result;
}
//==========================================================
Enter a string: remove duplicates from here
Final string is: remov duplicatsfh
Final string is: remov duplicatsfh