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...
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...
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...
Would you make separated this code by one .h file and two .c file? **********code************* #include...
Would you make separated this code by one .h file and two .c file? **********code************* #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include<time.h> // Prints out the rules of the game of "craps". void print_game_rule(void) { printf("Rules of the game of CRAPS\n"); printf("--------------------------\n"); printf("A player rolls two dice.Each die has six faces.\n"); printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n"); printf("After the dice have come to rest, the sum of the spots\n on the two upward faces is...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
. The attached file contains the six variables. I have already attempted this answer and got...
. The attached file contains the six variables. I have already attempted this answer and got it wrong. Please ignore the checkmarks. Question Using the information below select all of the variables that are dichotomous (i.e., two categories). QN88 QN33 _SMOKER3 _SLEPTIM1 QN44 _RFBING5 Behavioral Risk Factor Surveillance System (BRFSS 2016) Calculated Variables https://www.cdc.gov/brfss/annual_data/2016/pdf/2016_calculated_variables_version4.pdf Youth Risk Behavior Surveillance System (YRBSS 2015) YRBS Data User's Guide https://www.cdc.gov/healthyyouth/data/yrbs/pdf/2015/2015_yrbs-data-users_guide_smy_combined.pdf
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT