Anagrams

Hello Everybody!


The goal of this assignment is to write a program that arranges a list of words into separate lists of anagrams. Your program should read a text file which will be the input to your program and contains a list of words to be sorted into anagrams. The program should print to a file the lists of anagrams in the following way:
1. All the words that are anagrams of each other are displayed all on one line; words with no anagrams will be displayed alone.
2. The words on each line should be in alphabetical order.
3. Lines of words are sorted alphabetically according to the first word of a line.
4. Each word on a line should be separated by a space but otherwise there should be no spaces.


I wrote the following code but it's not working properly , and I still can't figure out how to write a print function.

Thanks for the help.

PS: it compiles perfectly


#include <iostream>
#include <algorithm>
#include <string>
#include <llist.h>
#include <fstream>

using namespace std;




template <typename E> class sortedLList: public LList<E> {
public:
sortedLList () : LList () {};

void insert (const E& it)

{


{head= new Link <E> (it, NULL);

}
}

void insert (const E& item, int r)
{

if (cnt==1)
{
if (r<0)
{insertAtStart (item);
}

else {append(item);
}
}
else{curr=head;
if (item<curr->element)
{insertAtStart(item);
}
else {
while(curr->next!=NULL&&item>curr->next->element){
curr=curr->next;}
//if (item<curr->next->element)

}

if (curr->next==NULL)
{append(item);}
else {curr->next=new Link <E> (item, curr->next); cout << item << " " ;}

}}





};

void findAnagram (string s)
{ sortedLList<char *> L1;
ifstream in;

int r;
int count=0;

char *t;
char *t2;
char* t1;
char* j;

string st,str2;
L1.cnt=0;

t=new char [s.size()+1];
strcpy (t,s.c_str()); //copies the string to char*

L1.insert(t);// first element of the list

std::sort(s.begin(),s.end()); //first word being sorted
t1=new char [s.size()+1];
strcpy (t1,s.c_str());

in.open ("anagramEasy.txt"); // open file from which we get the input
while (!in.fail())
{for (int i=0; i < count; i++)
in>>str2;

in>>st;
count ++;
t2=new char [st.size()+1];
strcpy (t2,st.c_str());
std::sort(st.begin(),st.end());
j=new char [st.size()+1];
strcpy (j,st.c_str());
if (strcmp(t1,j)==0) // compares two strings
{L1.cnt++;
r=strcmp(t2,t);

L1.insert(t2,r);}


}


}

void sortLList (sortedLList <char *> *l1, sortedLList<char *> *l2)
{
char * s1;
char * s2;
sortedLList<char *> *temp;

s1 = l1->head->element;
s2=l2->head->element;

if ( strcmp (s2,s1)<0)
{
temp->curr=l2->curr;
l2->curr=l1->curr;
l1->curr=temp->curr;
}
}


int main ()
{
string str;
sortedLList<char*> L1;
LList<sortedLList<char *>*> A;
ifstream inData;
inData.open ("anagramEasy.txt");
while (!inData.fail())
{

inData>>str;
findAnagram (str);

A.insert (&L1);

}

}



help anyone please?
Can you put your code in code tags [code ]like this[/code]?

there is no built-in printf() function in C++. You are probably talking about printf().
http://www.cplusplus.com/reference/cstdio/printf/

Though since you are already using std::basic_ios objects, I suggest you use cout:
http://www.cplusplus.com/doc/tutorial/basic_io/
Last edited on
All you have to do is write an algorithm that tells whether two words are anagrams; everything else is rudimentary.

1. Create a vector of vectors of type string (This is akin to a two-dimensional string array, where rows represent strings and columns their anagrams.).
2. Read a string.
3. If this is an anagram of an existing string, add it to this row. Otherwise, add it to a new row.
4. Sort the columns.
5. Sort the rows.

Edit: here's a quick sample I just wrote.

http://ideone.com/QBRlTj
Last edited on
I have to use linked list, and I have to create new function print that prints the sorted Linked list of sortedLList ..

thank you for your answers
Topic archived. No new replies allowed.