Code for sorting a char array?

Hi, so I was practicing the cin.get function and the question is that u input values in and the compiler will sort the letters out. However, when i run the code nothing happens, the compiler simply skips and moves on. Any help? It's supposed to work with the user writing a sentence with spaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
using namespace std;

int main()
{
	const int Size = 40;
	char Sentence[Size] = {};
	char LetterChange;
	cin.get(Sentence, Size);

	for (int i = 0; i < 40; i++)
	{
		for (int j = i + 1; j < Size; j++)
		{
			if (Sentence[j] > Sentence[i])
			{
				LetterChange = Sentence[i];
				Sentence[i] = Sentence[j];
				Sentence[j] = LetterChange;
			}
		}
	}
	cout << endl;

	return 0;
L23. You're not displaying the sorted Sentence:

 
cout << Sentence << endl;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int main()
{
   const size_t SIZE { 40 };
   
   char Sentence[SIZE];
   
   std::cin.get(Sentence, SIZE);
   std::cout << '\n';

   std::cout << Sentence << '\n';

   for (int i { }; i < SIZE; i++)
   {
      for (int j { i + 1 }; j < SIZE; j++)
      {
         if (Sentence[j] > Sentence[i])
         {
            char LetterChange = Sentence[i];
            Sentence[i]       = Sentence[j];
            Sentence[j]       = LetterChange;
         }
      }
   }
   std::cout << Sentence << "|\n"; // spaces get stuck at the end
                                   // need visual indicator to
                                   // show end of string
}
Hello World!

Hello World!
roollledWH! |

[ETA]:

At pointed out by seeplus below, sorting the entire char array instead of just the entered C string has problems.

After the user enters the sentence getting the length of the entered string should be done, and comparing against that value should be used as the terminating conditions of the for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstring>

int main()
{
   const size_t SIZE { 40 };

   char Sentence[SIZE];

   std::cin.get(Sentence, SIZE);
   std::cout << '\n';

   size_t str_size { strlen(Sentence) };

   std::cout << Sentence << '\n';

   for (int i { }; i < str_size; i++)
   {
      for (int j { i + 1 }; j < str_size; j++)
      {
         if (Sentence[j] > Sentence[i])
         {
            char LetterChange = Sentence[i];
            Sentence[i]       = Sentence[j];
            Sentence[j]       = LetterChange;
         }
      }
   }
   std::cout << Sentence << "|\n"; // spaces get stuck at the end
                                   // need visual indicator to
                                   // show end of string
}

Now the sorting is done only on the entered characters, with the added benefit of allowing for an ascending sort instead of a descending sort, change > to < in line 21. Sorting the entire C string in an ascending sort produces garbage.
Last edited on
Using <algorithm>'s std::sort to sort the C string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm> // std::sort
#include <array>     // std::begin / std::end

int main()
{
   const size_t SIZE { 40 };
   char Sentence[SIZE];

   std::cin.get(Sentence, SIZE);
   
   std::cout << Sentence << '\n';

   std::sort(std::begin(Sentence), std::end(Sentence),
             [ ] (char a, char b) { return a > b; } ); // lambda

   std::cout << Sentence << "|\n";
}
Same as above

The changes seeplus made below are correct, only the entered C string should be sorted. Not the entire char array.
Last edited on
The sort premise isn't right. sort (however done) should just sort the entered chars - not the whole of Sentence - especially where Sentence isn't initialised.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
#include <cstring>

int main() {
	const size_t SIZE {40};
	char Sentence[SIZE] {};

	std::cin.get(Sentence, SIZE);

	std::cout << Sentence << '\n';

	std::sort(Sentence, Sentence + strlen(Sentence),
		[](char a, char b) { return a > b; });

	std::cout << Sentence << "|\n";
}


But why c-style string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
#include <string>

int main() {
	std::string Sentence;

	std::getline(std::cin, Sentence);
	std::cout << Sentence << '\n';

	std::sort(Sentence.begin(), Sentence.end(),
		[](char a, char b) { return a > b; });

	std::cout << Sentence << "|\n";
}

Yeah, seeplus, I fooked up, not noticing* at the time I was doing the sorting of the C string in a wrong fashion. Mea culpa. Good catch of a stupid mistake.

Sorting the entire char array instead of just the entered C string produces garbage output when doing an ascending sort, while it appeared to work properly for the descending sort.

After changing the code to work on the actual size of the entered C string the change made it possible to sort a C string in descending and ascending order properly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstring>

int main()
{
   const size_t SIZE { 40 };

   char Sentence[SIZE];

   std::cout << "Enter a sentence (39 characters MAX!):\n";
   std::cin.get(Sentence, SIZE);

   size_t str_size { strlen(Sentence) };

   std::cout << "\nEntered sentence (" << str_size << " characters):\n";
   std::cout << Sentence << '\n';

   for (int i { }; i < str_size; i++)
   {
      for (int j { i + 1 }; j < str_size; j++)
      {
         if (Sentence[j] > Sentence[i])  // descending sort
         {
            char LetterChange = Sentence[i];
            Sentence[i]       = Sentence[j];
            Sentence[j]       = LetterChange;
         }
      }
   }

   std::cout << "\nSorted sentence:\n";
   std::cout << Sentence << "\n";

   for (int i { }; i < str_size; i++)
   {
      for (int j { i + 1 }; j < str_size; j++)
      {
         if (Sentence[j] < Sentence[i])  // ascending sort
         {
            char LetterChange = Sentence[i];
            Sentence[i] = Sentence[j];
            Sentence[j] = LetterChange;
         }
      }
   }

   std::cout << "\nSorted sentence:\n";
   std::cout << Sentence << "\n";
}
Enter a sentence (39 characters MAX!):
Hello World!

Entered sentence (12 characters):
Hello World!

Sorted sentence:
roollledWH!

Sorted sentence:
 !HWdellloor


*Well I knew something wasn't correct, I tried to do an ascending sort on the entire char array and got garbage results. For the life of me I couldn't figure out why.

I did do a test of a std::string sorted both ways and it worked. At the time I still didn't make the connection of the differences between a char array and the C string data. *annoyed grunt!*

Then before I saw your reply, seeplus, I figured it out. Still, have to give you credit for fixing my screw-up.
Last edited on
Topic archived. No new replies allowed.