470,850 Members | 1,012 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,850 developers. It's quick & easy.

Bubble sort for anagrams/words

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) {
count += 1;
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;
return words;
Oct 11 '06 #1
0 4183

Post your reply

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

Similar topics

13 posts views Thread by Gram | last post: by
34 posts views Thread by Mark Kamoski | last post: by
4 posts views Thread by Chris | last post: by
4 posts views Thread by Gaijinco | last post: by
reply views Thread by scruggsy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.