sorting an array by alphabetizing the char*
I'm not really sure what I am doing for my assignment, though I have spent the better part of 5 days trying to figure this project out. The outcome is to get the program to read the list of words off a text document, create a key for each word (ie. "letter" would have the key "eelrt") and then sort them if their keys match (matching anagrams together on one line). Currently I am working on the sorting part, but I quite honestly don't know what I'm doing...
Code:
#include <iostream>
char** read(const char* fileName, int& count);
class KeyedWord {
private:
char* _word;
char* _key;
public:
KeyedWord(char* word);
char* getWord() { return _word; }
char* getKey() { return _key; }
char* getAnagram() { return getKey(); }
};
KeyedWord::KeyedWord(char* word)
{
_word = word;
}
// sort by increasing anagram
void sort(KeyedWord** key, int numberOfObjects)
{
int arrayLength;
//finds length of string for each array
string strHold(key);
arrayLength = strHold.length();//sorts letters of word in alphabetical order to make key
for (int index = (arrayLength - 1); index >= 0; index--) {
for (int index2 = 1; index2 <= index; index2++) {
if (key[index2-index] > key[index]) {
char hold = key[index2-1];
key[index2-1] = key[index2];
key[index2] = hold;
} // end if
} // end for
} // end for
}
void printAnagrams(KeyedWord** keyedWords, int numberOfObjects)
{
int wordIndex = 0;
while (wordIndex + 1 < numberOfObjects) {
char* anagram = keyedWords[wordIndex]->getKey();
if (anagram == keyedWords[wordIndex + 1]->getKey()) {
std::cout << "Anagrams: " << std::endl;
std::cout << " " << keyedWords[wordIndex]->getWord();
std::cout << " " << keyedWords[wordIndex + 1]->getWord();
wordIndex += 1;
while (wordIndex + 1 < numberOfObjects && anagram == keyedWords[wordIndex + 1]->getKey()) {
std::cout << " " << keyedWords[wordIndex + 1]->getWord();
wordIndex += 1;
}
std::cout << std::endl;
} else {
wordIndex += 1;
}
}
}
void main(int argumentCount, char** arguments)
{
if (argumentCount <= 1) {
std::cout << "No file name given as argument" << std::endl;
} else {
int numberOfWords = 0;
int testArraySize = 6;
char** words = read(arguments[1], numberOfWords);
char* testWords[6] = {"RUDE", "WORD", "DURE","FROM", "FORM", "LETTER" };
// allocate the arrays of KeyedWord objects
KeyedWord** keyedWords = new KeyedWord* [numberOfWords];
KeyedWord** testKeyedWords = new KeyedWord* [6];
// populate the array of keyedWord objects
for (int wordIndex = 0; wordIndex < numberOfWords; ++wordIndex)
{
keyedWords[wordIndex] = new KeyedWord(words[wordIndex]);
}
//populate the array of testKeyedWords objects
for (int wordIndex = 0; wordIndex < testArraySize; ++wordIndex) {
testKeyedWords[wordIndex] = new KeyedWord(testWords[wordIndex]);
std::cout << testWords[wordIndex] << std::endl;
}
sort(testKeyedWords, testArraySize);
printAnagrams(testKeyedWords, testArraySize);
/*/tests ability to read words.txt file
for(int i = 0; i < numberOfWords; i++) {
std::cout << words[i]<< std::endl;
if (i == 200)
system ("pause");
else if (i == 400)
system ("pause");
else if (i == 600)
system ("pause");
else if (i == 800)
system ("pause");
else if (i == 1000)
system ("pause");
}
std::cout << std::endl;
system ("pause");*/
//sort(keyedWords, numberOfWords);
//printAnagrams(keyedWords, numberOfWords);
for (int wordIndex = 0; wordIndex < numberOfWords; wordIndex++) {
delete keyedWords[wordIndex];
}
delete [] keyedWords;
system("pause");
}
}
I'm sorry about the code being so jammed together, I'm not quite sure how to post it properly yet. so I highlighted in blue the part I am working on, the test file of 6 words. If I can get that far, I should be able to do the .txt file.
Originally it sorted only the initial first letter to the front of the line, then quit, and now there is some kind of error with the identifier of "string" for "strHold(key)"?? Any help would be greatly appreciated. I have heard something about a squot? or something...but I honestly don't know enough about built in functions to grasp it...
Thanks in advance for the help!
~Shadow