- #1
zorro
- 1,384
- 0
Can some one explain me the difference between the following two pieces of code ?
First one doesn't work. I debugged it and made it to work but I am unable to understand how.
The lines with changes have been made red bold.
First one doesn't work. I debugged it and made it to work but I am unable to understand how.
The lines with changes have been made red bold.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 100
int hash_func(char k[]);
void hash(int key,char k1[],char k2[]);
char *search(int key,char k[]);struct hash_table{
[B] [COLOR="Red"]char *cword;
char *mword;[/COLOR][/B]
struct hash_table *next;
};
struct hash_table *arr[SIZE];
int main() {
char k1[100];
char k2[100];
char buf[200];
char test[100];
char *word;
int key;
int i;
int choice1;
int choice2;
FILE *fp1;
FILE *fp2;
fp1=fopen("spell.txt","r");
if(fp1==0){
printf("Could not open dictionary file");
exit(0);
}
for(i=0;i<SIZE;i++){
arr[i]=NULL;
}
while(fscanf(fp1,"%s",k1)!=EOF){
fscanf(fp1,"%s",k2);
key=hash_func(k1);
hash(key,k1,k2);
printf("%d %s %s\n",key,arr[key]->cword,arr[key]->mword);
}
printf("%s\n",arr[56]->mword);
printf("Enter 0 for auto spell check and 1 for manual check\n");
scanf("%d",&choice1);
for(i=0;i<100;i++){
if(arr[i]!=NULL)
printf("%d >>%s\n",i,arr[i]->cword);
}
while(fscanf(fp2,"%s",k1)!=EOF){
key=hash_func(k1);
word=search(key,k1);
if(choice1==0){
//fprintf(fp2,"%s",k2);
printf("--%s--",word);
}
else{
printf("Misspelled Word : %s Correct Word : %s\n",k1,k2);
printf("Enter 0 to modify the word in file\n");
scanf("%d",&choice2);
if(choice2==0)
fprintf(fp2,"%s",k2);
}
}
}
int hash_func(char k[]){
int n;
int sum=0;
n=strlen(k);
while(n--){
sum=sum+*k++;
}
printf("key %d\n",sum%100);
return sum%100;
}
void hash(int key,char k1[],char k2[]){
struct hash_table *temp,*new;
temp=arr[key];
if(temp==NULL){
arr[key]=(struct hash_table *)malloc(sizeof(struct hash_table));
[B][COLOR="Red"]arr[key]->cword=k2;
arr[key]->mword=k1;[/COLOR][/B]
arr[key]->next=NULL;
}
else{
while(temp->next!=NULL)
temp=temp->next;
new=(struct hash_table *)malloc(sizeof(struct hash_table));
[B][COLOR="Red"]new->mword=k1;
new->cword=k2;[/COLOR][/B]
new->next=NULL;
temp->next=new;
}
}
char * search(int key,char k[]){
struct hash_table *temp;
if(arr[key]==NULL){
printf("*1*");
return k;
}
else{
temp=arr[key];
while(temp->next!=NULL){
if(strcmp(k,temp->mword)==0){
printf("*2*");
return temp->cword;
}
temp=temp->next;
}
}
}
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int hash_func(char k[]);
void hash(int key,char k1[],char k2[]);
char *search(int key,char k[]);struct hash_table{
[B] [COLOR="Red"]char cword[20];
char mword[20];[/COLOR][/B]
struct hash_table *next;
};
struct hash_table *arr[100];
int main() {
char k1[100];
char k2[100];
char buf[200];
char test[100];
char *word;
int key;
int i;
int choice1;
int choice2;
FILE *fp1;
FILE *fp2;
fp1=fopen("spell.txt","r");
if(fp1==0){
printf("Could not open dictionary file");
exit(0);
}
for(i=0;i<100;i++){
arr[i]=NULL;
}
while(fscanf(fp1,"%s",k1)!=EOF){
fscanf(fp1,"%s",k2);
key=hash_func(k1);
hash(key,k1,k2);
printf("%d %s %s\n",key,arr[key]->cword,arr[key]->mword);
}
printf("%s\n",arr[56]->mword);
printf("Enter 0 for auto spell check and 1 for manual check\n");
scanf("%d",&choice1);
for(i=0;i<100;i++){
if(arr[i]!=NULL)
printf("%d >>%s\n",i,arr[i]->cword);
}
while(fscanf(fp2,"%s",k1)!=EOF){
key=hash_func(k1);
word=search(key,k1);
if(choice1==0){
//fprintf(fp2,"%s",k2);
printf("--%s--",word);
}
else{
printf("Misspelled Word : %s Correct Word : %s\n",k1,k2);
printf("Enter 0 to modify the word in file\n");
scanf("%d",&choice2);
if(choice2==0)
fprintf(fp2,"%s",k2);
}
}
}
int hash_func(char k[]){
int n;
int sum=0;
n=strlen(k);
while(n--){
sum=sum+*k++;
}
printf("key %d\n",sum%100);
return sum%100;
}
void hash(int key,char k1[],char k2[]){
struct hash_table *temp,*new;
temp=arr[key];
if(temp==NULL){
arr[key]=(struct hash_table *)(malloc(sizeof(struct hash_table)));
[B] [COLOR="Red"]strcpy(arr[key]->cword,k2);
strcpy(arr[key]->mword,k1);[/COLOR][/B]
arr[key]->next=NULL;
}
else{
while(temp->next!=NULL)
temp=temp->next;
new=(struct hash_table *)malloc(sizeof(struct hash_table));
[B] [COLOR="Red"]strcpy(new->mword,k1);
strcpy(new->cword,k2);[/COLOR][/B]
new->next=NULL;
temp->next=new;
}
}
char * search(int key,char k[]){
struct hash_table *temp;
if(arr[key]==NULL){
printf("*1*");
return k;
}
else{
temp=arr[key];
while(temp->next!=NULL){
if(strcmp(k,temp->mword)==0){
printf("*2*");
return temp->cword;
}
temp=temp->next;
}
}
}