Sample Input files input.txt: golf foxtrot echo delta charlie beta alpha SAMPLE
ID: 3634865 • Letter: S
Question
Sample Input filesinput.txt:
golf
foxtrot
echo
delta
charlie
beta
alpha
SAMPLE OUTPUT
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
#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;
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;
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)
{
break;
}
}
strcpy(Dictionary[i].word, result);
if(wordsCountedSoFar <= i + 1)
wordsCountedSoFar = i + 1;
if( Dictionary[i].lineNumber == 0)
Dictionary[i].lineNumber = fileLine;
/** store the distinct words **/
result = strtok( NULL, delims );
}
}
fclose(inputFile);
for(i = 0; i < 1000; ++i)
{
if(Dictionary[i].lineNumber > 0)
printf("word %s found at line:%d ", Dictionary[i].word, Dictionary[i].lineNumber);
}
return 0;
}
Explanation / Answer
#include #include #include #define WS " , !:;.-" #define MAX_STR_LEN 1024 struct listNode { char *word; struct listNode *next; }; struct listNode *newListNode(const char * const); void insertWord(struct listNode *,const char * const); void deleteList(struct listNode *); void printList(struct listNode *); int main(int argc, char *argv[]) { FILE *fp; char line[MAX_STR_LEN], *s; struct listNode *head = newListNode(""); if ((argc > 1) && ((fp = fopen(argv[1],"r")) != NULL)) { while (fgets(line,MAX_STR_LEN,fp) != NULL) { *(strchr(line,' ')) = ''; for (s = strtok(line,WS); s != NULL; s = strtok(NULL,WS)) { insertWord(head,s); } } printf(" %s sorted: ",argv[1]); printList(head); deleteList(head); fclose(fp); } else { if (argc < 2) { printf("usage: %s ",argv[0]); } else { printf("%s not found. ",argv[1]); } } return 0; } /* * newListNode: create new struct listNode */ struct listNode *newListNode(const char * const s) { struct listNode *n = (struct listNode*)calloc( 1,sizeof( struct listNode ) ); n->word = (char *)calloc( strlen(s)+1,sizeof( *s ) ); strcpy(n->word,s); n->next = NULL; return n; } /* * insertWord: insert given word into list in ascending order */ void insertWord(struct listNode *head,const char * const s) { struct listNode *p = head, *q = newListNode(s); while ((p->next != NULL) && (strcmp(s,p->next->word) > 0)) { p = p->next; } q->next = p->next; p->next = q; } /* * deleteList : free all memory allocated for the list */ void deleteList(struct listNode *head) { struct listNode *p = head, *q; while (p != NULL) { q = p->next; free(p->word); free(p); p = q; } } /* * printList: display the list */ void printList(struct listNode *head) { struct listNode *p = head->next; while (p != NULL) { printf("%s ",p->word); p = p->next; } puts(""); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.