How do I correct the program below to search all the anagrams in a given file.
First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for "word", or "eelttr" for "letter".
Build an array of all the keys, and sort it using a bubble sort. I have to write a modified version of bubble sort that maintains also the array of the primary words.
For instance, if I had ["rude", "word", "dure", "from", "form", "letter"],
our initial array of keys would be:
["deru", "dorw", "deru", "fmor", "fmor", "eelttr"]
After the sort, we should have the keys as:
["deru", "deru", "dorw", "eelltr", "fmor", "fmor"]
and the words
["rude", "dure", "word", "letter", "from", "form"]
I need to go through the array of keys to find out duplicated keys, and the corresponding words. Or how can I do this with struct and arrays? Thanks in advance.
#include <iostream>
#include <fstream>
char** read(const char* fileName, int& count)
{
std::ifstream countingStream(fileName);
// first count them
count = 0;
while (true) {
char line[100];
countingStream.getline(line, 100);
if (strlen(line) == 0) {
break;
}
count += 1;
}
countingStream.close();
std::ifstream readingStream(fileName);
char** words = new char* [count];
for (int index = 0; index < count; ++index) {
char line[100];
readingStream.getline(line, 100);
words[index] = strdup(line);
std::cout << line << std::endl;
}
readingStream.close();
return words;
}