Sort the given string using character search | C ++ implementation to sort the given string without using any sorting technique ; A character array to store the no . of occurrences of each character between ' a ' to ' z ' ; to store the final sorted string ; To store each occurrence of character by relative indexing ; To traverse the character array and append it to new_str ; Driver program to test above
#include <iostream> NEW_LINE using namespace std ; string sortString ( string str , int n ) { int i ; char arr [ 26 ] = { 0 } ; string new_str = " " ; for ( i = 0 ; i < n ; i ++ ) ++ arr [ str [ i ] - ' a ' ] ; for ( i = 0 ; i < 26 ; i ++ ) while ( arr [ i ] -- ) new_str += i + ' a ' ; return new_str ; } int main ( ) { string str = " geeksforgeeks " ; int n = str . size ( ) ; cout << sortString ( str , n ) ; return 0 ; }