DO NOT USE AI! AI WILL NOT PASS THE TEST!   Part I: Define a “word” as a maximal length sequence of alphanumerics. For example, in the line   Hello, there! How are you? I am fine; how are you? My number is DE4-3656.   the words are “hello”, “there”, “How”, “are”, “you”, “I” “am”, “fine”, “how”, “are”, “you”, “My”, “number”, “is”, “DE4”, and “3646”.   Write a C program that reads lines from a file (you may assume it’s a text file) or files named on the command line, breaks them into words, stores the words in order (that is, ASCII order), and counts the number of times each word appears. Do not make any assumptions about the number of lines or words; but you may assume no word is over 256 characters long. When this is done, print the words and counts, as follows. This example is from the line above: 3646 (1) DE4 (1) Hello (1) How (1) I (1) My (1) am (1) are (2) fine (1) how (1) is (1) number (1) there (1) you (2) Call your file wordsort1.c. Hint: This is easiest done using a linked list, with each element a structure containing a pointer to the word and count. Part II: Now modify the wordsort1.c so that it prints the words in dictionary order rather than ASCII order. Dictionary order, sometimes called lexicographic order, intermixes upper-case and lower-case letters, so the upper-case letter precedes the corresponding lower-case letter. Digits precede all letters. Here is the same list as in the example in problem 1, but in lexicographic order: 3646 (1) am (1) are (2) DE4 (1) fine (1) Hello (1) How (1) how (1) I (1) is (1) My (1) number (1) there (1) you (2) Call your file wordsort2.c.   Part III: The “word” was defined as any sequence of alphanumerics. But this did not take into account words split over two lines. For example,   hel- lo   is really the word “hello”. The two parts are separated by a hyphen, perhaps some space, a newline, and perhaps more space. For this part, take your program wordsort1.c and modify it so it will handle words that are split over 2 lines, as above. below were the code I have for Part I and it return the following error massage, you could use this code or debug this code and write Part II and Part III based on part I.  wordsort1-1 (0/7) Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0) wordsort1-2 (0/7) Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0) wordsort1-3 (0/7) Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0) wordsort1-4 (0/7) Test Failed: 0 != 1 : Incorrect exit status (got 0 but expected 1) wordsort1-5 (0/7) Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0)   Code I have: Part I:  #include #include #include #include #define MAX_WORD_LENGTH 256 typedef struct WordNode {     char* word;     int count;     struct WordNode* next; } WordNode; WordNode* createNode(char* word) {     WordNode* newNode = (WordNode*)malloc(sizeof(WordNode));     newNode->word = strdup(word);     newNode->count = 1;     newNode->next = NULL;     return newNode; } void insertWord(WordNode** head, char* word) {     if (*head == NULL || strcmp((*head)->word, word) > 0) {         WordNode* newNode = createNode(word);         newNode->next = *head;         *head = newNode;     }     else if (strcmp((*head)->word, word) == 0) {         (*head)->count++;     }     else {         insertWord(&(*head)->next, word);     } } void printWordList(WordNode* head) {     while (head != NULL) {         printf("%s (%d)\n", head->word, head->count);         head = head->next;     } } void freeWordList(WordNode* head) {     WordNode* temp;     while (head != NULL) {         temp = head;         head = head->next;         free(temp->word);         free(temp);     } } void processFile(const char* filename) {     FILE* file = fopen(filename, "r");     if (file == NULL) {         printf("Failed to open file: %s\n", filename);         return;     }     char line[MAX_WORD_LENGTH];     WordNode* wordList = NULL;     while (fgets(line, sizeof(line), file)) {         char* token = strtok(line, " \t\n");         while (token != NULL) {             insertWord(&wordList, token);             token = strtok(NULL, " \t\n");         }     }     fclose(file);     printWordList(wordList);     freeWordList(wordList); } int main(int argc, char* argv[]) {     if (argc < 2) {         printf("Usage: ./wordsort1 \n");         return 1;     }     int i;     for (i = 1; i < argc; i++) {         processFile(argv[i]);     }     return 0;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

DO NOT USE AI! AI WILL NOT PASS THE TEST!

 

Part I: Define a “word” as a maximal length sequence of alphanumerics. For example, in the line

 

Hello, there! How are you? I am fine; how are you? My number is DE4-3656.

 

the words are “hello”, “there”, “How”, “are”, “you”, “I” “am”, “fine”, “how”, “are”, “you”, “My”, “number”, “is”, “DE4”, and “3646”.

 

Write a C program that reads lines from a file (you may assume it’s a text file) or files named on the command line, breaks them into words, stores the words in order (that is, ASCII order), and counts the number of times each word appears. Do not make any assumptions about the number of lines or words; but you may assume no word is over 256 characters long. When this is done, print the words and counts, as follows. This example is from the line above:



3646 (1)

DE4 (1)

Hello (1)

How (1)

I (1)

My (1)

am (1)

are (2)

fine (1)

how (1)

is (1)

number (1)

there (1)

you (2)

Call your file wordsort1.c.

Hint: This is easiest done using a linked list, with each element a structure containing a pointer to the word and count.



Part II: Now modify the wordsort1.c so that it prints the words in dictionary order rather than ASCII order. Dictionary order, sometimes called lexicographic order, intermixes upper-case and lower-case letters, so the upper-case letter precedes the corresponding lower-case letter. Digits precede all letters.

Here is the same list as in the example in problem 1, but in lexicographic order:



3646 (1)

am (1)

are (2)

DE4 (1)

fine (1)

Hello (1)

How (1)

how (1)

I (1)

is (1)

My (1)

number (1)

there (1)

you (2)

Call your file wordsort2.c.

 

Part III: The “word” was defined as any sequence of alphanumerics. But this did not take into account words split over two lines. For example,

 

hel-

lo

 

is really the word “hello”. The two parts are separated by a hyphen, perhaps some space, a newline, and perhaps more space.

For this part, take your program wordsort1.c and modify it so it will handle words that are split over 2 lines, as above.

below were the code I have for Part I and it return the following error massage, you could use this code or debug this code and write Part II and Part III based on part I. 

  1. wordsort1-1 (0/7)
    Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0)
  2. wordsort1-2 (0/7)
    Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0)
  3. wordsort1-3 (0/7)
    Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0)
  4. wordsort1-4 (0/7)
    Test Failed: 0 != 1 : Incorrect exit status (got 0 but expected 1)
  5. wordsort1-5 (0/7)
    Test Failed: -11 != 0 : Incorrect exit status (got -11 but expected 0)

 

Code I have: Part I: 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORD_LENGTH 256

typedef struct WordNode {
    char* word;
    int count;
    struct WordNode* next;
} WordNode;

WordNode* createNode(char* word) {
    WordNode* newNode = (WordNode*)malloc(sizeof(WordNode));
    newNode->word = strdup(word);
    newNode->count = 1;
    newNode->next = NULL;
    return newNode;
}

void insertWord(WordNode** head, char* word) {
    if (*head == NULL || strcmp((*head)->word, word) > 0) {
        WordNode* newNode = createNode(word);
        newNode->next = *head;
        *head = newNode;
    }
    else if (strcmp((*head)->word, word) == 0) {
        (*head)->count++;
    }
    else {
        insertWord(&(*head)->next, word);
    }
}

void printWordList(WordNode* head) {
    while (head != NULL) {
        printf("%s (%d)\n", head->word, head->count);
        head = head->next;
    }
}

void freeWordList(WordNode* head) {
    WordNode* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp->word);
        free(temp);
    }
}

void processFile(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        printf("Failed to open file: %s\n", filename);
        return;
    }

    char line[MAX_WORD_LENGTH];
    WordNode* wordList = NULL;

    while (fgets(line, sizeof(line), file)) {
        char* token = strtok(line, " \t\n");
        while (token != NULL) {
            insertWord(&wordList, token);
            token = strtok(NULL, " \t\n");
        }
    }

    fclose(file);

    printWordList(wordList);
    freeWordList(wordList);
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("Usage: ./wordsort1 <filename>\n");
        return 1;
    }
    int i;
    for (i = 1; i < argc; i++) {
        processFile(argv[i]);
    }

    return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education