Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prompt the user for the name of an input text file Read the input text file and

ID: 3647515 • Letter: P

Question


Prompt the user for the name of an input text file
Read the input text file and display a keyword index for the contents that shows the line number of the first occurrence of each unique word in the file
Display the keyword index

Notes:

You may assume that the input file contains no more than 1000 distinct words
A word is a sequence of non-whitspace characters delimited by whitespace, as per the C convention

Sample Output

Program: Structs
Enter an input file name: input.txt
The input file contains 7 distinct words
alpha, 7
beta, 6
charlie, 5
delta, 4
echo, 3
foxtrot, 2
golf, 1

[done]

Sample input file input.txt:
golf
foxtrot
echo
delta
charlie
beta
alpha

cant figure out whats wrong with it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define fileNameLength 256

typedef struct {
char word[256];
int lineNumber;
} word;

int main()
{
FILE *inputFile;
char inputFileName[256] = {0};
int fileLine = 0;
int wordsCountedSoFar = 0;
int i, j;
char bufferWord[256];
char bufferChar;
char *result = NULL;
char delims[] = " ";
int foundWord = 0;
word Dictionary[1000];
printf("Program: Structs ");

do
{
printf("Enter an input file name: ");
fgets(inputFileName, fileNameLength, stdin);
inputFileName[strlen(inputFileName) - 1] = '';
if ((inputFile = fopen(inputFileName, "r")) == NULL)
{
printf("Unable to open "%s": %s ", inputFileName,
strerror(errno));
}
} while (inputFile == NULL);

for(i = 0; i < 1000; ++i)
{

Dictionary[i].lineNumber = 0;
}
while (fgets(bufferWord, 256, inputFile) )
{
++fileLine;
foundWord = 0;
printf(" words found in line %d, %s ",fileLine, bufferWord);
bufferWord[strlen(bufferWord) -1] = '';

result = strtok( bufferWord, delims );
while( result != NULL ) {
printf( ""%s" ", result );

for(i = 0; i < wordsCountedSoFar; ++i)
{

if(strcmp(Dictionary[i].word, result) == 0)
{
foundWord = 1;
break;
}

else if(strcmp(Dictionary[i].word, result) > 0)
{

for(j = wordsCountedSoFar; j >= i; j--)
{
strcpy(Dictionary[j+1].word, Dictionary[j].word);
Dictionary[j+1].lineNumber = Dictionary[j].lineNumber;
}
break;
}
}


if(foundWord == 0)
{
strcpy(Dictionary[i].word, result);
wordsCountedSoFar += 1;
Dictionary[i].lineNumber = fileLine;
}


result = strtok( NULL, delims );
}
}
fclose(inputFile);

printf(" The input file contains %d distinct words ", wordsCountedSoFar);

for(i = 0; i < 1000; ++i)
{
if(Dictionary[i].lineNumber > 0)
printf(" word %s found at line: %d", Dictionary[i].word, Dictionary[i].lineNumber);
}

printf(" ");
return 0;

}



Explanation / Answer

there is nothing wrong with it it is ok in my compiler