C++ Program to Sort Characters in String
You can sort characters in a string in C++ by using
std::sort(str.begin(), str.end()) which rearranges the characters in ascending order.Examples
Inputstring
Outputginrst
Inputhello
Outputehllo
Inputa
Outputa
How to Think About It
To sort characters in a string, think of the string as a list of letters. You want to rearrange these letters from smallest to largest based on their order in the alphabet. Using a sorting function, you compare letters and swap them until the whole string is ordered.
Algorithm
1
Get the input string from the user.2
Use a sorting method to reorder the characters in the string.3
Print the sorted string as output.Code
cpp
#include <iostream> #include <algorithm> #include <string> int main() { std::string str; std::cin >> str; std::sort(str.begin(), str.end()); std::cout << str << std::endl; return 0; }
Output
ehllo
Dry Run
Let's trace the input "hello" through the code
1
Input string
str = "hello"
2
Sort characters
After std::sort, str = "ehllo"
3
Print output
Output: ehllo
| Index | Before Sort | After Sort |
|---|---|---|
| 0 | h | e |
| 1 | e | h |
| 2 | l | l |
| 3 | l | l |
| 4 | o | o |
Why This Works
Step 1: Using std::sort
The std::sort function rearranges elements between two iterators in ascending order.
Step 2: String iterators
We use str.begin() and str.end() to tell std::sort which part of the string to sort.
Step 3: In-place sorting
The sorting happens inside the original string, so no extra string is needed.
Alternative Approaches
Using a vector and std::sort
cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::string str; std::cin >> str; std::vector<char> chars(str.begin(), str.end()); std::sort(chars.begin(), chars.end()); for (char c : chars) std::cout << c; std::cout << std::endl; return 0; }
This method uses extra memory for the vector but is useful if you want to manipulate characters separately.
Using counting sort for only lowercase letters
cpp
#include <iostream> #include <string> int main() { std::string str; std::cin >> str; int count[26] = {0}; for (char c : str) count[c - 'a']++; for (int i = 0; i < 26; i++) { for (int j = 0; j < count[i]; j++) std::cout << char('a' + i); } std::cout << std::endl; return 0; }
This is faster for strings with only lowercase letters but less flexible.
Complexity: O(n log n) time, O(1) space
Time Complexity
The sorting function std::sort uses a fast sorting algorithm (usually introsort) which runs in O(n log n) time where n is the string length.
Space Complexity
Sorting is done in-place on the string, so extra space is O(1).
Which Approach is Fastest?
Using std::sort is generally fastest and simplest. Counting sort is faster only for limited character sets like lowercase letters.
| Approach | Time | Space | Best For |
|---|---|---|---|
| std::sort on string | O(n log n) | O(1) | General strings with any characters |
| Vector + std::sort | O(n log n) | O(n) | When needing separate character manipulation |
| Counting sort | O(n) | O(1) | Strings with only lowercase letters |
Use
std::sort with str.begin() and str.end() to quickly sort string characters.Forgetting to include
<algorithm> header causes compilation errors when using std::sort.