Hello. I am writing program which should find the longest word in .dat file. Here is my code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int i=0,a=0;
FILE *in_file;
char ch;
char word[20],longest_word[20],str[200];
if((in_file = fopen("word.dat", "r"))==NULL)
{
printf("File %s cannot be openned\n", "words.dat");
system("pause");
exit(8);
}
else
{
while (!feof(in_file))
{
fgets(str, 199 , in_file);
while (str[a]!= '\n')
{
ch = str[a];
if (((int)ch>=97 && (int)ch<=122)||((int)ch>=65 && (int)ch<=90))
{
word[i]=ch;
i++;
}
else
{
if ((strlen(word))>(strlen(longest_word)))
{
strcpy ( word, longest_word );
i=0;
}
}
}
a=0;
}
}
printf ("%s \n",longest_word);
system("pause");
fclose(in_file);
}
I don't know what is wrong with the code i think it should work but i am not getting any output. Maybe someone could help me?