Question

In: Computer Science

assignment in C I have a file that contains a lot of lines with words separated...

assignment in C
I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.

Solutions

Expert Solution

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;
}

Related Solutions

Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g. 2.0,1 5.5,2 10,0 5.1,9.6 Write...
The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g. 2.0,1 5.5,2 10,0 5.1,9.6 Write a program which reads the file using the Python CSV module, creates a list from the contents, then divides the first number in each line by the second. Define a function in your program that performs the division operations; the function must accept two numeric parameters, divide the first parameter by the second, and return the result. Before dividing, your function must validate the...
I have a C++ question. I'm reading words from a text file and storing each words...
I have a C++ question. I'm reading words from a text file and storing each words in an array. Currently I'm doing it like the code below and its working fine but I need all the words to be stored in the array as lower case letters. How can I do this? Thank you. if(file.is_open())     {         for(int i = 0; i < arrayLength; i++)         {             file >> array[i];         }     }
The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
the assignment folder for this assignment contains a file called values.txt. The first line in the...
the assignment folder for this assignment contains a file called values.txt. The first line in the file contains the total number of integers which comes after it. The length of this file will be the first line plus one lines long. Implement a MinHeap. Your program should read in input from a file, add each value to the MinHeap, then after all items are added, those values are removed from the MinHeap. Create a java class called MinHeap with the...
how to creat Create a text file that contains a list of integer numbers separated by...
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp...
Write a program to reverse the lines of a file and to reverse the words plus...
Write a program to reverse the lines of a file and to reverse the words plus the letter's of each word within each line using ArrayList. A file name mobydick.txt Example: Original file contains the following MOBY DICK; OR THE WHALE by Herman Melville CHAPTER 1 Loomings. Out put should be .sgnimooL 1 RETPAHC ellivleM namreH yb ELAHW EHT RO ;KCID YBOM its for java eclipse
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
A hotel salesperson enters sales in a text file. Each line contains the following, separated by...
A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Write a program that reads such a file and displays the total amount for each service category. Use the following data for your text file:5 pts Bob;Dinner;10.00;January 1, 2013 Tom;Dinner;14.00;January 2, 2013 Anne;Lodging;125.00;January 3, 2013 Jerry;Lodging;125.00;January...
Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT