A function sort
has to sort letters in string. But WITHOUT using built-in C++ sort()
function! I have tried different methods but nothing helped. Here are the arguments in this function. Need to sort text
. (Is it possible to this without removing &
before text
?)
This code goes into endless loop.
std::string TextUtility::sort(const std::string &text)
{
string toSort = text;
string sorted_str;
int size = sizeof(toSort) / sizeof(char);
for (int i = 0; i < size - 1; i++)
{
if (toSort[i] > toSort[i + 1])
{
int temp = toSort[i + 1];
toSort[i + 1] = toSort[i];
toSort[i] = temp;
i = -1;
}
}
for (int i = 0; i < toSort.size(); i++)
{
sorted_str += toSort[i];
}
cout << endl
<< "Sorted: " << sorted_str;
return sorted_str;
}