How To Sort A String In C++ | 2 Different Ways

Sorting is very important in Competitive Programming. Here, we are going to discuss How to sort a string in C++ in 2 different ways. Sorting String refers to Sorting Multiple String or Sorting characters of One String. Anyway, I will discuss sorting of the one string according to their characters.

What is Sorting?

Before Begin with Sorting in C let’s have a look on Sorting Definition

Arranging Particular set in Given Order is Known As Sorting

Sorting refers to arranging, However, It Does not refer to only Increasing or decreasing order. Arranging according to Given Order or condition refers to Sorting.

Different Ways To Sort String In C++

There are basically two ways of Sorting in C++, both are given below.

  • Using Sorting Techniques
  • Using C++ STL Library

Sorting Techniques are basically Bubble Sort, Insertion Sort, Merge Sort and other Sorting Techniques whereas C++ STL library refers to use sort() function which is discussed in detail.

Using Sorting Techniques

There are multiple Sorting Techniques like Bubble sorting, merge Sorting, Quick Sorting. But In this post I am going to discuss Bubble sort.

Bubble Sorting to Sort String in C++:

This is one of the simplest sorting algorithm of sorting. What this algorithm states that,

Compare two Adjacent element and swap according to given condition

Let’s take an example you have a string named “GEEKS“. Now I am going to perform the Bubble Sorting in “GEEKS“.

  1. We compare [G E E K S] and changed it to [E G E K S], We swap G with E because E comes before G
  2. Again We compare [E G E K S] and changed it to [E E G K S]
  3. Now Comparing [E E G K S] but we can not swap G and K because they are in the right order already.
  4. We again compare [ E E G K S] but again we can’t swap because K and S are in right order

After all this, we get S as our Greatest Character in terms of position in Alphabets. I know this is sorted character but bubble sort did not know that.

Now Bubble Sort again works and Find out the second most greater character and this is done for an n-1 time where n is the number of characters in the string.

After all phase of Sorting we will get [E E G K S]

Program For Sorting all characters of One Single String

We used Bubble sorting for sorting all characters in string in bubblesort Function

#include <iostream>
#include <string>
using namespace std;

// A function to implement bubble sort 
void bubbleSort(string str, int n) 
   int i, j; 
   for (i = 0; i < n-1; i++)    
       for (j = 0; j < n-i-1; j++)  
           if (str[j] > str[j+1]) 
                 char temp = str[j+1];
                 str[j+1] = str[j];
                 str[j] = temp;
        cout<<"\n String after sorting: "<<str<<" \n";
int main ()
    string str;
    cout<<"Enter the string to be sorted: ";
    int len = str.length();
    cout<<"\n String before sorting: "<<str<<" \n";

    return 0;

And the Output is given below.

 Enter the string to be sorted: 5
 String before sorting: GEEKS 

 String after sorting: EEGKS 

Note: Above program only work if String is only in Capital Letter or Small Letter

Using In Build Sort Function

In this method, we use the inbuild function present in C++ STL for sorting known as Sort. Sort function uses the Quick Sort algorithm as their algorithm technique. The sort function is very easy to use and also perform searching operation fast.

Below is the implementation of sort function

Syntax Of Sort

sort(starting address, ending address);

Program to Sort String in C++ Using Sort method

#include <bits/stdc++.h>
using namespace std;

int main ()
    string str;
    cout<<"Enter String"<<endl; // Assume Entered string is "geekstocode"
    cout<<"Sorted String "<<endl;

    return 0;

Here we use str.begin() which gives the address of str[0] and str.end() which gives the last address of the string. Hereby default sort function sorts the string in ascending order and the output is


If you want to sort the string in descending order, then after sorting you can create a new string and copy the resultant string from the last index. This is how to sort a string in C++.

Problem Set Related to String Sorting

Sort Array Of String

Top 20 String Interview Problem

C Equal String

Count Binary String

Reverse Engineering Problem

getline in C

Leave a Comment

Your email address will not be published. Required fields are marked *