Sort a String in C++

 Sort String in C++ using Inbuilt Function

If we are given a string of lowercase characters from 'a' to 'z' and we have to sort the string, we can use the inbuilt function available in the C++ STL without writing codes.

For Example: raunitverma

For Example: codingkaro

To do this we can simply sort the string as we use to sort a vector in C++ using the inbuilt function.

using namespace std;
int main() {
    string str="codingkaro";
    sort(str.begin(),str.end());   //Sorting the given String using inbuilt function
return 0;

codingkaro acdgiknoor

Else what you can do is that you can choose the smallest character in the string then add it to new string and then delete the previous character from the string, In this way you can also sort the string.

Post a Comment