return new string...

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
> 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?
so how can i do this mission?
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
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
Bur in all these cases the order of symbols is brocken.
closed account (3qX21hU5)
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
Topic archived. No new replies allowed.