By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

Bubble sort for anagrams/words

P: 2
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;
}
Oct 11 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.