Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:36:57+00:00 2026-06-13T04:36:57+00:00

The program basically searches for strings in a given wordlist that are anagrams of

  • 0

The program basically searches for strings in a given wordlist that are anagrams of the word that is input from the terminal.
It follows the algorithm:

  1. Sort all the words in the list
  2. Calculate hash values of the sorted words so that all anagrams have the same hash value
  3. Create the hash table and start chaining by storing the hash value, the sorted word and the actual word
  4. Find the anagrams by checking the hash table

Now the problems is that it runs perfectly on my machine but not on others’ computers. I am including the code here and providing a link for the wordlist. I know it is too much to ask you guys to download the word list and then compile and check but it will mean a lot if you let me know. I am running Ubuntu 11.04 with 2.6.38-13-generic-pae

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

typedef struct x
{
int hashvalue;
char dictword[100];
char ascending[100];
struct x *next;
} node;

char *sortWord(char *);
//double fact(int);
void main()
{
FILE *fp1, *fp2;
char ch1;
while(ch1!='q')
{   
char *dictsort;
dictsort=(char *)malloc(100*sizeof(char));  
fp1=fopen("wordlist.txt", "r");
char ch;
int i=0;
char *sortedword;
int hashindex;
long int n;
printf("Please enter size of hashtable or press Ctrl+C to break:\n");
scanf("%ld", &n);


 node *hashtable[n];
 node *temp;
 char test1[50];
char *test;     
printf("Please enter the word to find anagrams for:\n");
scanf("%s", test1);
test=sortWord(test1);
int testhash=hashfunction(test);
printf("Hash value of word is %d\n", testhash);
for(i=0;i<1000000;i++)
{
    hashtable[i]=NULL;
}
temp=NULL;
while(!feof(fp1))
{   //printf("Seg fault here...\n");        
    //ch=getc(fp1);     
    fgets(dictsort, (100*sizeof(char)),fp1);
    //puts(dictsort);

    sortedword=(char *)malloc(sizeof(dictsort));
    sortedword = sortWord(dictsort);
    hashindex=hashfunction(sortedword);


    if(hashtable[hashindex]==NULL){ 

        hashtable[hashindex] = (node *)malloc(sizeof(node));
        strcpy(hashtable[hashindex]->dictword,dictsort);
        strcpy(hashtable[hashindex]->ascending,sortedword);
        hashtable[hashindex]->hashvalue=hashindex;
        hashtable[hashindex]->next=NULL;        
    }else{

        temp = (node *)malloc(sizeof(node));
        strcpy(temp->dictword,dictsort);
        strcpy(temp->ascending,sortedword);
        temp->hashvalue=hashindex;
        temp->next=hashtable[hashindex];
        hashtable[hashindex]=temp;
    //  free(temp);             
    }
    //printf("%s", hashtable[hashindex]->dictword);
    free(sortedword);

}

//for(i=0;i<100000;i++)
//{ 
    node *print;
    print=(node *)malloc(sizeof(node));
    print=hashtable[testhash];
    int chk;        

    while(print!=NULL)
    {
        chk=strcmp(print->ascending,test);
        if(chk==0)              
            {
                printf("%s\n", print->dictword);
                print=print->next;
            }   

        else
        print=print->next;
    }



    free(print);

}
}

int hashfunction(char *sw){

int a=0,i=0;
int k,b,c;
int div=1000000;
int blah;
int hv;     
for(i=0;sw[i]!='\0';i++){

    a=sw[i];
    //b=sw[i+1];
    //c=sw[i+2];        
    if(a!=10&&b!=10)
    {
        k=a*fact(i);
        b=k;            

        hv+=b;  
    }

}
hv=hv%div;  
return hv;

}

char *sortWord(char *s)
{
int c, d = 0, length;
char *pointer, *result, ch;
FILE *fp;
length = strlen(s);

result = (char*)malloc(length+1);

pointer = s;

for ( ch = 'a' ; ch <= 'z' ; ch++ )
{
        for ( c = 0 ; c < length ; c++ )
        {
            if ( *pointer == ch )
            {
                    *(result+d) = *pointer;
                    d++;
            }
        pointer++;
            }
    pointer = s;
}
*(result+d) = '\0';
char *z;
z=(char *)malloc(length+1);
strcpy(z, result);

//fp=fopen("sortedlist.txt", "a");
//fprintf(fp, "%s\n", s);

//fclose(fp);

free(result);

//puts(s);
//return result;
return z;   
//free(result); 


}

int fact(int num)
{
int i;
int val=1;
for(i=num+1;i>=1;i--)
{
    val=val*i;
}

return val%1000000;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:36:58+00:00Added an answer on June 13, 2026 at 4:36 am

    Among a lot of other things:

     int hv;
     /* ... */
     hv+=b;
    

    hv is never initialized. (Well as isn’t object b in the same function and as isn’t object ch1 is main function.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have a program that filters records from one excel file to another
I currently have a program that basically reads html from a webpage. After sending
Basically this program searches a .txt file for a word and if it finds
I'm writing a Python program for Linux and the program basically uses the terminal
I've got a program made basically from a ListCtrl and has an add button,
Right what im trying to accomplish is a program that basically sets the active
Basically I want to make simple toggle program (that will be mapped to some
Basically I've written this program to check for strings. I've used socket method for
I'm writing a program that basically perform server-client relationship. When i run my client
My current project is to write a program that basically is a cash register

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.