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
Output:
For Example: codingkaro
Output:
To do this we can simply sort the string as we use to sort a vector in C++ using the inbuilt function.
#include<bits/stdc++.h>
using namespace std;
int main() {
string str="codingkaro";
cout<<str<<endl;
sort(str.begin(),str.end()); //Sorting the given String using inbuilt function
cout<<str<<endl;
return 0;
}
OUTPUT:
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.
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.