Sorting Alphabetically

Hi guys,

The exercise I am attempting is requiring me to create dynamic arrays, which the user selects the size of. The part I am stuck on is sorting the strings alphabetically. Do you have any hints on how to go about this?

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
#include "Header.h"
#include <iostream>
#include <string>
using namespace std;

//Task 1: Alphabetical
int main()
{
	int size;

	cout << "Enter the size of an array: ";
	cin >> size;
	cin.ignore(); //important

	string *Array = new string[size]; //Array of pointers
	for (int i = 0; i < size; i++)
	{
		cout << "\n Enter word: ";
		getline(cin, Array[i]);
	}

  

	system("PAUSE");
	return 0;
}
Strings, by default, can be compared with each other since they have operators to allow for it. That should make things a bit easier already.
For instance, the following program will print
"hello" is less than "world"
, because the first letter of 'string_one' ('h') has a numerical, decimal value of 104, and the first character of 'string_two' ('w') has a numerical, decimal value of 119. 104 is less than 119.
You can find a list these values by searching for "ASCII table" or here: http://www.asciitable.com/

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

int main() {
	
	std::string string_one = "hello";
	std::string string_two = "world";

	if (string_one < string_two) {
		std::cout << "\"" << string_one << "\" is less than \"" << string_two << "\"" << std::endl;
	}

	return 0;
}


Note, that upper- and lower-case letters have different values. In the ASCII table, the upper-case characters appear before the lower-case ones, which means a string such as "ABC" will be less than "abc", or even "aBC" or "abC". The way the comparison works is effectively by comparing the first characters of both strings, then the second characters of both strings, then the third, and so on.
Last edited on
So in c++ strings automatically only compare the first index only?

I tested this out, and it seems so...I just wanted to confirm
So in c++ strings automatically only compare the first index only?

It's done the same way as it is done in a dictionary. For example, "hell" comes before "hello".

Also, you forgot to delete[] your array.

As for sorting the array, the easiest method would be to use the bubble sort algorithm.
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
50
51
52
53
54
55
56
57
58

#include "Header.h"
#include <iostream>
#include <string>
using namespace std;

//Task 1: Alphabetical
int main()
{
	int size = 0;

	cout << "Enter the size of an array: ";
	cin >> size;
	cin.ignore(); //important

	string *Array = new string[size]; //Array of pointers
	for (int i = 0; i < size; i++)
	{
		cout << "\n Enter word: ";
		getline(cin, Array[i]);
	}

	cout << endl;

	int j = 0;
	bool swap = true;
	string temp;
	while (swap)
	{
		swap = false;
		j++;
		for (int l = 0; l < size - j; l++)
		{
			if (Array[l] > Array[l + 1])
			{
				temp = Array[l];
				Array[l] = Array[l + 1];
				Array[l + 1] = temp;
				swap = true;
			}
		}
	}
	
	for (int k = 0; k < size; k++)
	{
		cout << Array[k] << endl;
		 
	}

	cout << endl << endl;

	delete[] Array;

	system("PAUSE");
	return 0;
}

 


Thanks for all the help!
Topic archived. No new replies allowed.