Jan 9, 2013 at 8:58pm UTC
hello
i have to write a function that get a string,and return a new string with only one copy of every kind of char from the original string/
example:
if the original string is; a,b,c,a,b
the new string should be contain: a,b,c
how i can do it?
i think to sort the strings chars with bubble-sort,but i dont know how to continue from this point.
tnx
Jan 9, 2013 at 9:36pm UTC
> i think to sort the strings chars with bubble-sort,
There are better sorting algorithms
> but i dont know how to continue from this point.
¿why are you sorting then?
Jan 9, 2013 at 9:46pm UTC
so how can i do this mission?
Jan 9, 2013 at 11:40pm UTC
I think you should copy the original string into the destination string character by character and check whether the destination string already contains the given character.
Last edited on Jan 9, 2013 at 11:41pm UTC
Jan 10, 2013 at 12:23am UTC
You could call sort() and then call unique():
1 2 3 4 5
std::string f(std::string s)
{
std::sort(s.begin(), s.end());
return {s.begin(), std::unique(s.begin(), s.end())};
}
You could construct a set and then a string:
1 2 3 4 5
std::string f(const std::string& s)
{
std::set<char > set(s.begin(), s.end());
return {set.begin(), set.end()};
}
or use any of the multiple other methods. What are your real goals? What do you have available?
Last edited on Jan 10, 2013 at 12:26am UTC
Jan 10, 2013 at 12:45am UTC
Bur in all these cases the order of symbols is brocken.
Jan 10, 2013 at 1:46am UTC
Increment through the string character by character, every time you increment you can test that character to see if it is already in the new string if its not add it to the string.
Last edited on Jan 10, 2013 at 2:31am UTC