I was trying to sort a file of 1000 unsorted mixed case words into alphabetical order.
Example: Ancient absent Bow bar Cow cat ...
Give the code below:
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
void bubbleSort(string[],int);
int main() {
const int size = 1000;
string words[size];
ifstream input;
int index;
input.open("List of 1000 Mixed Case Words.txt");
if (input.fail())
{
cout << "Error opening file." << endl;
}
else
{
for (int i = 0; i < size; i++)
{
getline(input, words[i]);
}
}
bubbleSort(words, size);
for (int i = 0; i < size; i++)
{
cout << words[i] << endl;
}
cout << "Enter the index: ";
cin >> index;
cout << endl;
cout << "The word on that index is " << words[index] << endl;
}
void bubbleSort(string arr[],int size) {
int maxEle;
int index;
for ( maxEle = size -1; maxEle > 0; maxEle--)
{
for ( index = 0; index < maxEle; index++)
{
if (arr[index] > arr[index +1])
{
swap(arr[index], arr[index + 1]);
}
}
}
}
The result is : Abraham Lincoln Actinium Afghanistan Barbados Barium Belarus Belgium ... Zimbabwe Zinc Zirconium absence abundance ban banana band ... vaccine woman writer zebra
I'm just starting C++ and this problem is too hard for me right now. How I can sort the file like the example?