I want to read program which takes word from user and find anagrams for them in file txt. I read this but I have a problem how to read a file and then propertly compare two words. What I do wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <conio.h>
bool if_anagram(char *a, char *b)
{
int dl1 = strlen(a), dl2 = strlen(b);
if(dl1!=dl2)return false;
int licz[127]={};
for(int i=0;i<dl1;i++)
licz[a[i]]++;
for(int i=0;i<dl1;i++)
licz[b[i]]--;
for(int i=0;i<127;i++)
if(licz[i]!=0)
return false;
return true;
}
int main(int argc, char *argv[]) {
char a[101], b[8], * buffer;
int i=8, n;
FILE *pFile;
pFile=fopen("text.txt","r");
printf("Put word: ");
scanf ("%s", a);
buffer = (char*) malloc (i+1);
if (buffer==NULL) exit (1);
if (pFile != NULL){
while(!feof(pFile))
{
for (n=0; n<i; n++)
buffer[n]=fgets(b,8,pFile);
buffer[i]='\0';
printf("%s\n", b);
//printf("%p\n", &b);
if(if_anagram(a,b))
printf("Word %s is anagram!", b);
free (buffer);
}
fclose(pFile);
}
else{
printf("File is not exist.\n");
}
return 0;
}