Midsem Assignment
Midsem Assignment
ROLL : 21011A0555
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct data{
char word[50];
int count;
int probes;
}d[1035];
int tableSize=1035;
void display();
char *hash[1035];
void strip_punct(char * p) {
char *ptr=p;
while (*src)
src++;
/* Make it lowercase */
src++;
src++;
dst++;
else
/* Copy character */
*dst++ = *src++;
*dst = 0;
int main()
{
FILE *fptr;
char path[100];
int x=1;
int choice;
char words[MAX_WORDS][50];
char word[50];
int count[MAX_WORDS];
FILE *ft;
int ch;
ft = fopen(name, "r+");
if (ft == NULL)
exit(1);
if (ch == '-')
fputc(' ',ft);
fseek(ft, 0, SEEK_CUR);
}
}
fclose(ft);
if (fptr == NULL)
exit(EXIT_FAILURE);
count[i] = 0;
index = 0;
//Remove punctuation
strlwr(word);
strip_punct(word);
//printf("%s",word);
len = strlen(word);
if (ispunct(word[len - 1]) )
word[len - 1] = '\0';
if (ispunct(word[0]) )
{
for(i=0;i<strlen(word);i++){
word[i]=word[i+1];
word[i]='\0';
isUnique = 1;
if (strcmp(words[i], word) == 0)
isUnique = 0;
if (isUnique)
strcpy(words[index], word);
count[index]++;
index++;
else
count[i - 1]++;
// Close file
fclose(fptr);
int occur=0;
hashing(hashFun(words[i]),words[i],count[i]);
occur++;
while(x){
printf("\n1-Display\n2-Search\n3-Exit\n");
printf("Enter Choice:\n");
scanf("%d",&choice);
switch(choice){
case 1:
display();
break;
case 2:
scanf("%s",word);
search(word);
break;
case 3:
x=0;
break;
default:
printf("Enter correctly");
return 0;
}
int hashKey;
hashKey=(sum(words))%tableSize;
return hashKey;
int i;
int ascii=0;
for(i=0;words[i]!='\0';i++){
ascii=ascii+words[i];
return ascii;
int probes=1;
if(hash[hashKey]!=0 &&hashKey!=tableSize){
while(hash[hashKey]!=0){
hashKey++;
probes++;
if(hashKey==tableSize){
hashKey=0;
if(hash[hashKey]!=0){
while(hash[hashKey]!=0){
hashKey++;
probes++;
}
hash[hashKey]=words;
d[hashKey].count=count;
d[hashKey].probes=probes;
void display(){
int i;
for(i=1;i<tableSize;i++){
Probes:%d\n",i,hash[i],d[i].count,d[i].probes);
{ int cmp = 1;
//printf("%d",index);
int i = index ;
while(hash[i]!= NULL)
{ if(strcmp(hash[i],key1)==0)
return;
i = (i+1)%tableSize;
cmp++;
if(i==index)
{
printf("Not found after %d comparisions\n",cmp);
return;
if(hash[i]==NULL){
printf("Not found" );
OUTPUT :
OUTPUT :
1-Display
2-Search
3-Exit
Enter Choice:
2-Search
3-Exit
Enter Choice:
stranger
Comparisons Made:1
1-Display
2-Search
3-Exit
Enter Choice:
joe
Not found
1-Display
2-Search
3-Exit
Enter Choice:
heaven
Comparisons Made:409
1-Display
2-Search
3-Exit
Enter Choice:
--------------------------------