Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the different characters present in a string

Tags:

c++

string

Is there any way to find the all the unique characters present in a string without finding all the occurrences of the string ? For example, Let it be string a="mississippi" , the output should be {i,m,p,s}. Is there any inbuilt functions to find that in c++?

like image 305
Fakru Avatar asked Feb 03 '26 15:02

Fakru


1 Answers

You can do that using std::sort, std::unique, std::string::erase

Note : original string will be modified [If you don't want that make a copy of it]

std::string str = "mississippi";
std::sort(std::begin(str), std::end(str));
auto last = std::unique(std::begin(str), std::end(str));
str.erase(last, std::end(str));
like image 134
Praveen Avatar answered Feb 05 '26 05:02

Praveen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!