Question

In: Computer Science

Could you write a c- program that reads a text file into a linked list of...

Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements

1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise

2. Replace "sh" with ph

This is the text to be manipulated:

Paragraph1

She told us to take the trash out.

Why did she do that?

I wish she would not do that

Paragraph 2

We came home very late despite the fact we had school the next day.

We had a lot of fun

I miss my friends.

In c programming language please

Solutions

Expert Solution

C code that reads a text file, create a linked list of characters from file then replaces characters as per instructions.

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

typedef struct node {
    char val;
    struct node * next;
} node_t;

void print_list(node_t * head) {
    node_t * current = head;

    while (current != NULL) {
        printf("%c", current->val);
        current = current->next;
    }
}

void push_back(node_t * head, char val) {
    node_t * current = head;
    while (current->next != NULL) {
        current = current->next;
    }

    /* now we can add a new variable */
    current->next = (node_t *) malloc(sizeof(node_t));
    current->next->val = val;
    current->next->next = NULL;
}

void replace_chars(node_t* head) {
    node_t* current = head;
    node_t* prev = head;
    
    if (head != NULL) {
        prev = current;
        current = current->next;
        
        while (current != NULL) {
            if (tolower(current->val) == 's' || tolower(current->val) == 'e' || tolower(current->val) == 'y') {
                if (tolower(prev->val) == 'c') prev->val = (char)((int)prev->val + 16);
            }
            if (tolower(current->val) == 'h' && tolower(prev->val) == 's') prev->val = (char)((int)prev->val - 3);
            prev = current;
            current = current->next;
        }
    }
}

int main() {
    // open file
    FILE *fp;
    fp = fopen("test.txt", "r");
    
    // crate head of linked list
    node_t * head = NULL;
    head = (node_t *) malloc(sizeof(node_t));
    
    // reads single character from file
    char c = fgetc(fp); 
    head->val = c;
    
    // run this while loop till we reach EOF (end of file)
    while (c != EOF) {
        // read a character
        c = fgetc(fp);
        // add latest character to end of linked list
        if (c != EOF) push_back(head, c);
    }
    
    replace_chars(head);
    
    print_list(head);
    
    return 0;
}

File - test.txt

Output:

Please refer this screenshot for indentation help:

If you need any help, please leave a comment. If you are happy with my answer, please consider giving a like.


Related Solutions

Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT