In: Computer Science
C++ Lexicographical Sorting
Given a file of unsorted words with mixed case: read the entries in the file and sort those words lexicographically. The program should then prompt the user for an index, and display the word at that index. Since you must store the entire list in an array, you will need to know the length. The "List of 1000 Mixed Case Words" contains 1000 words.
You are guaranteed that the words in the array are unique, so you don't have to worry about the order of, say, "bat" and "Bat."
For example, if the array contains ten words and the contents are
cat Rat bat Mat SAT Vat Hat pat TAT eat
after sorting, the word at index 6 is Rat
You are encouraged to use this data to test your program.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
==============================
// mixedCaseWords.txt (input file)
cat Rat bat Mat SAT Vat Hat pat TAT eat Bat
==============================
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
bool isUnique(string words[],int cnt,string word);
void sort(string words[],int cnt);
int main() {
const int SIZE=1000;
string words[SIZE];
string word;
int cnt=0,index;
//Declaring variables
ifstream dataIn;
dataIn.open("mixedCaseWords.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//Reading the data from the file
while(dataIn>>word)
{
bool b=isUnique(words,cnt,word);
if(!b)
{
words[cnt]=word;
cnt++;
}
}
dataIn.close();
sort(words,cnt);
while(true)
{
cout<<"Enter Index :";
cin>>index;
if(index<0 || index>cnt)
{
cout<<"** Invalid.Must be
between 0-"<<cnt<<" **"<<endl;
}
else
break;
}
cout<<"After sorting, the word at index
"<<index<<" is "<<words[index]<<endl;
}
return 0;
}
bool isUnique(string words[],int cnt,string word)
{
string str1,str2;
for(int
k=0;k<word.length();k++)
{
str2+=tolower(word.at(k));
}
for(int i=0;i<cnt;i++)
{
str1="";
for(int
j=0;j<words[i].length();j++)
{
str1+=tolower(words[i].at(j));
}
if(str1.compare(str2)==0)
{
return
true;
}
}
return false;
}
void sort(string array[],int cnt)
{
//This Logic will Sort the Array of
elements in Ascending order
string temp,mixedTemp;
string str1="";
string lower[cnt];
for(int m=0;m<cnt;m++)
{
for(int
s=0;s<array[m].length();s++)
{
str1+=tolower(array[m].at(s));
}
lower[m]=str1;
str1="";
}
for (int i = 0; i < cnt; i++)
{
for (int j = i + 1; j < cnt; j++)
{
if (lower[i].compare(lower[j])>0)
{
temp = lower[i];
lower[i] = lower[j];
lower[j] = temp;
mixedTemp=array[i];
array[i]=array[j];
array[j]=mixedTemp;
}
}
}
}
===========================================
Output:
=====================Could you plz rate me well.Thank You