In: Computer Science
The main.c file is given below: Input File Name- input.txt, Output File Name- output.txt
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <getopt.h>
typedef struct node
{
        int count;
        char *word;
        struct node *next;
} Node;
Node *makeNode(Node *head, char *word)
{
        Node *current = NULL;
        current = malloc(sizeof(Node));
        current->word = malloc(strlen(word) + 1);
        strcpy(current->word, word);
        current->next = NULL;
        current->count = 1; // initialize the count to be
        head->next = current;
        return current;
}
static void printList(Node *head)
{
        if (head != NULL)
        {
                printf("%-10s   %d\n", head->word, head->count);
                head = head->next;
                printList(head);
        }
}
static void printListFile(Node *head, FILE *output)
{
        if (head != NULL)
        {
                fprintf(output, "%-10s   %d\n", head->word, head->count);
                head = head->next;
                printListFile(head, output);
        }
}
Node *findNodeForWord(Node *head, char *word)
{
        if (head->next == NULL)
        {
                Node *insertNode = makeNode(head, word); //insert after the head
                return insertNode;
        }
        else if (strcmp(head->next->word, word) == 0)
        { //stuff in the list
                head->next->count++;
                return head->next;
        }
        else if (strcmp(head->next->word, word) < 0)
        { // list word is less than given word
                head = head->next;
                findNodeForWord(head, word);
        }
        else if (strcmp(head->next->word, word) > 0)
        {
                Node *linkNode = head->next;
                Node *insertedNode = makeNode(head, word);
                insertedNode->next = linkNode;
                return insertedNode;
        }
}
static void addWord(Node *head, char *word)
{
        Node *nodeForWord = findNodeForWord(head, word);
}
int main ( int argc, char *argv[]) {
        char c;
        int i, j = 0;
        char buffer[100];
        //initialize the list with appropriate values
        Node *list = malloc(sizeof(Node));
        list->next = NULL;
        list->count = 0;
        int fileOutput = 1;
        int fileInput = 1;
        char *fileName = NULL;
        FILE *src = NULL;
        FILE *output = NULL;
    src = fopen("input.txt", "r");
    output = fopen ("output.txt", "w" );
        int opt;
        if (fileInput == 1) {
                for (i = 0; (c = fgetc(src)) != EOF; ++i) {
                        if (isalpha(c))
                                buffer[j++] = tolower(c);
                        else {
                                buffer[j++] = '\0';
                                addWord(list, buffer);
                                j = 0;
                        }
                }
        } else {
                while ((c = getchar()) != EOF) {
                        if (isalpha(c))
                                buffer[j++] = tolower(c);
                        else {
                                buffer[j++] = '\0';
                                addWord(list, buffer);
                                j = 0;
                        }
                }
        }
        if(fileOutput == 1) {
                printListFile(list->next->next, output);
        }else {
                printList(list->next->next);
        }
        fclose (src); // close the file
        fclose (output);
        return 0;
}