+ 3

How to sort any string??

if i take in any string via cin, it should return the sorted string..Can we do it without using the #include<algortihm> ?

18th Feb 2018, 6:03 AM
Dew See 33
Dew See 33 - avatar
3 Answers
+ 8
Sorted, as in "acbed" returns "abcde"? E.g. std::string sort(std::string literal) { for (int i = 0; i < literal.length(); i++) for (int j = 0; j < literal.length() - 1 - i; j++) if (literal[j] > literal[j + 1]) { char temp = literal[j]; literal[j] = literal[j + 1]; literal[j + 1] = temp; } return literal; }
18th Feb 2018, 8:13 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Sure. Turn it into a char array and implement any sorting algorithm by hand
18th Feb 2018, 7:09 AM
1of3
1of3 - avatar
0
And if you don't want to use std::sort, you may use qsort from cstdlib to sort the string, though you will have to convert it to a char array first.
20th Feb 2018, 4:32 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar