+ 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> ?
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;
}
+ 2
Sure. Turn it into a char array and implement any sorting algorithm by hand
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.