1

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?

gin13789
  • 13
  • 3
  • 2
    yeah, but what's the question? – Dee Mar 11 '21 at 03:42
  • 1
    `if (arr[index] > arr[index +1])` is comparing strings lexicographically. You need to convert what that compares to a unified case (upper or lower) if you want `absence` to come before `Barbados`, etc. You can do it here and heat a dining hall with the wasted CPU, or do it once during the read-in and heat small utility room. It is, after all, still bubblesort, so efficiency is already thrown out the window. – WhozCraig Mar 11 '21 at 03:44
  • Expanding on WhozCraig's answer: see https://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case for a discussion how to convert a string to lower case. – Daniel Junglas Mar 11 '21 at 05:25
  • 1
    some more explanation : the ASCII value of 'a' (97) is higher than of 'A' (65) that explains what you see. – AndersK Mar 11 '21 at 06:01

1 Answers1

0

if only first letter is uppercase, you can use this function:

bool gt(string a, string b){
    if(a[0] < 97)
        a[0] += 32;
    if(b[0] < 97)
        b[0] += 32;

    return a>b;
}

then. instead of this

if (arr[index] > arr[index +1])

you put this

if (gt(arr[index] , arr[index +1]))

and this should work for you. Otherwise, just convert the entire word to lower or uppercase.

Dharman
  • 30,962
  • 25
  • 85
  • 135
robbinc91
  • 548
  • 6
  • 14
  • Can you explain how `bool gt()` function work? – gin13789 Mar 11 '21 at 14:51
  • ASCII code for 'a' is 97, and ASCII for 'A' is 65. All uppercase letters are below 'a' (97). So, assuming my input consists only of ASCII characters (specifically english letters), if a character ASCII code is below 97 you can assume it is uppercase. In function gt I just converted first letter to lowercase by adding 32. (97 - 65 = 32), and then compared the two strings – robbinc91 Mar 11 '21 at 14:53