-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalphabetical_order.cpp
More file actions
38 lines (34 loc) · 1.37 KB
/
alphabetical_order.cpp
File metadata and controls
38 lines (34 loc) · 1.37 KB
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
//Create an array of strings and display them in alphabetical order.
#include <iostream>
using namespace std;
int main() {
// Create an array of strings
int size;
cout<<"Enter size of array: ";
cin>>size;
string strings[size];
for (int i=0;i<size;i++)
{
cout<<"Enter "<<i+1<<" element: ";
cin>>strings[i];
}
// Calculate the number of strings in the array
int numStrings = sizeof(strings) / sizeof(strings[0]);
// Simple sorting algorithm (Bubble Sort) to sort the strings alphabetically
for (int i = 0; i < numStrings - 1; i++) {
for (int j = 0; j < numStrings - i - 1; j++) {
if (strings[j] > strings[j + 1]) {
// Swap the strings if they are out of order
string temp = strings[j];
strings[j] = strings[j + 1];
strings[j + 1] = temp;
}
}
}
// Display the sorted strings
cout << "Strings in alphabetical order:" <<endl;
for (int i = 0; i < numStrings; i++) {
cout << strings[i] << endl;
}
return 0;
}