Define a function
string sort(string s)
that returns a string with the same characters as s, but arranged in ascending order, according to ascii value . For example sort("hello") will return the string "ehllo". Notice that the function returns a different string from the original argument, since s is not passed by reference.
Use the following algorithm: Initialize a temporary variable t to the empty string . Now find minIndex: the index of the smallest character c in s. Append s[minIndex] to t and then call s.erase(minIndex, 1). Repeat this process until s is empty. Return t.
Here is my solution:
string sort(string s)
{
string t = "";
char c;
int minIndex = 0;
for(int i = 0; i < s.length() && s[i] != c; i++)
{
if(s[i] != "")
{
s[minIndex] = t;
s.erase(minIndex, 1);
}
}
return t;
}
MyProgrammingLab is saying that this is not the correct solution. Is there anything I need to add or fix in this sort function? Any help would be appreciated.