// APS105 Lab6 11/05/06
// Geoffrey Lang
// 995155772
// langgeof@ecf.utoronto.ca
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_WORDS = 10000;
const int MAX_WORD_LENGTH = 25;
bool goodLetter(char a);
void printArray(char words[][MAX_WORD_LENGTH], int n);
void selectionSort(char words[][MAX_WORD_LENGTH], int n);
int deleteDuplicates(char words[][MAX_WORD_LENGTH], int n);
int main (int argc, char *argv[])
{
char current;
int i = 0, j = 0;
bool twolines = 0;
char words[MAX_WORDS][MAX_WORD_LENGTH];
char temp[MAX_WORD_LENGTH];
ifstream in_stream; // Opens the file written after the
in_stream.open(argv[1]); // name of this program when it is run
while(!in_stream.eof())
{
in_stream.get(current);
current = tolower(current);
if((current == ' ' || current == '\n') && twolines == 1)// Move to next line if there is
{ // a space in the input
while(!goodLetter(words[i][strlen(words[i])-1]))
{
words[i][strlen(words[i])-1] = '\0';
}
i++;
j = 0;
twolines = 0;
}
if(j == 0 && goodLetter(current) || j != 0) // Check that the first letter isn't punctuation
{ // before putting the word into the array and
words[i][j] = current; // if its not the first letter, put it in anyway
j++;
twolines = 1;
}
}
// selectionSort(words,i); // Apply selection sort to the array
// and then print it (for testing
printArray(words,i); // purposes only)
// i = deleteDuplicates(words,i); // Delete the duplicates in the array
// and print it to check that they are
// printArray(words,i-1); // all deleted
return 0;
}
bool goodLetter(char a) // Function to check if a character is
{ // a character that can start or end
if((a >= 'a' && a <= 'z') || (a >= '0' && a <= '9')) // a word--must be a letter or a
{ // number. Returns 1 if the character
return 1; // is a letter or number, 0 if not
}
else
{
return 0;
}
}
void printArray(char words[][MAX_WORD_LENGTH], int n) // Function to print an array, one
{ // word per line
for(int x = 0; x < n; x++)
{
for (int y = 0; words[x][y] != '\0'; y++)
{
cout << words[x][y];
}
cout << endl;
}
return;
}
void selectionSort(char words[][MAX_WORD_LENGTH], int n) // Function to sort an array of
{ // c-strings in alphabetical
for(int i = 0; i < n - 1; i++) // order
{
int smallest = i;
for(int j = i+1;j < n; j++)
{
if(strcmp(words[j],words[smallest]) < 0)
{
smallest = j;
}
}
char temp[1][MAX_WORD_LENGTH];
strncpy(temp[0],words[i],MAX_WORD_LENGTH);
strncpy(words[i],words[smallest],MAX_WORD_LENGTH);
strncpy(words[smallest],temp[0],MAX_WORD_LENGTH);
}
return;
}
int deleteDuplicates(char words[][MAX_WORD_LENGTH], int n) // Function to delete all duplicate
{ // enteries in an array of sorted
int i=0, j=0, removed=0; // c-strings
while(i < n - 1 )
{
while (strncmp(words[i-j],words[i+1],MAX_WORD_LENGTH) == 0)
{
j++;
i++;
}
if (j > 0)
{
strncpy(words[i-j+1],words[i+1],MAX_WORD_LENGTH);
j--;
}
removed++;
}
return removed - 1;
}