-3

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;
}
Ðаn
  • 10,934
  • 11
  • 59
  • 95
Alina
  • 424
  • 1
  • 4
  • 17
  • 2
    *"Is it possible to this without removing `&` before `text`?"* -- Sure. The `string toSort = text;` defeats the whole purpose of the const reference anyway. – Blaze Feb 11 '20 at 15:43
  • Rather than deleting a question and posting a near identical one with additional details, you can [edit the original](https://stackoverflow.com/posts/60172373/edit) to add the details. – 1201ProgramAlarm Feb 11 '20 at 15:43
  • Please [do not post duplicate questions](https://stackoverflow.com/questions/60172373/sort-letters-in-string-alphabetically-without-using-built-in-sort). Also, you'll be surprised to learn that `sizeof(toSort)` gives the same result whether your string is empty or contains the entirety of "War And Peace". Your C++ book should have many examples of obtaining a string's length. – Sam Varshavchik Feb 11 '20 at 15:43
  • Sorry, I don't have much experience in StackOverflow. – Alina Feb 11 '20 at 15:44
  • 4
    Strings have a `size()` member. `sizeof(toSort) / sizeof(char);` is completely wrong. – interjay Feb 11 '20 at 15:44
  • If you "don't have much experience in StackOverflow", this is what the [help] is for, where you can take a [tour] of stackoverflow.com and learn [ask] questions here. – Sam Varshavchik Feb 11 '20 at 15:45
  • @Alina [See this on how to implement sorting algorithms without std::sort](https://stackoverflow.com/questions/24650626/how-to-implement-classic-sorting-algorithms-in-modern-c) – PaulMcKenzie Feb 11 '20 at 16:12

1 Answers1

0

After cleaning up (see comments) this seems to work just fine.

#include <string>
#include <iostream>

std::string sort(const std::string &text)
{
  std::string toSort = text;

  for (int i = 0; i < (int)(toSort.size()) - 1; i++)
  {
    if (toSort.at(i) > toSort.at(i + 1))
    {
      char temp = toSort.at(i + 1);
      toSort.at(i + 1) = toSort.at(i);
      toSort.at(i) = temp;
      i = -1;
    }
  }

  std::cout << "Sorted: " << toSort << std::endl;

  return toSort;
}

int main()
{
  sort("fgfghafbkgdbGHKGHjbkgfnvnbjb14563f");
  sort("X");
  sort("");
  return(0);
}

Sorted: 13456GGHHKabbbbbdfffffgggghjjkknnv

Sorted: X

Sorted:

BTW: The explicit cast (int)(toSort.size()) is needed, as an empty string would break the condition. At least on my system it would lead to "i < size_t(-1)" which causes std::out_of_range. Maybe something like that caused your endless loop?

SKCoder
  • 409
  • 3
  • 10