Question

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

Homework Answers

Answer #1
//header file included for standard input output
#include<stdio.h>
//header file included for string operations
#include<string.h>
//header file included for dynamic memory manipulation
#include<stdlib.h>

//structure of node i.e. linked list is defined.
struct node{
    //data for storing char type data
    char data;
    //next pointer points node of same structure type 
    struct node *next;
};

//cFlag to check whether 'c' is followed by the characters 'e', 'i' or 'y'
//initially 0 indicates that 'c' is not followed by the characters 'e', 'i' or 'y'
int cFlag=0;

//pointer to linked list start and last are declared
struct node *start=NULL;
struct node *last=NULL;

//prevChar to store previous character
char prevChar;

//insert() to insert file data into linked list
void insert(char c){
    //temp of node type is dynamically created
    struct node *temp = (struct node *)malloc(sizeof(struct node));

    if(temp==NULL){
        printf("\nMemory Full.");
        return;
    }

    //temp's data is assigned with file's data
    temp->data = c;
    temp->next = NULL;

    //if start is NUll i.e. linked list is empty
    //then make this initial temp node as start and end
    if(start==NULL){
        start = temp;
        last = temp;
        prevChar = c;
        return;
    }

    //cFlag is checked
    if(cFlag==0){
        //if condition satisfied then cFlag will be set to 1
        if((prevChar == 'c') && (c=='e'||c=='i'||c=='y')){
            cFlag = 1;
        }
    }

    
    prevChar = c;

    //file's data are inserted in the linked list's end 
    //so that insertion takes O(1) time to insert data at end of linked list
    last->next = temp;
    last = temp;
}

//processParagraphs() to process the linked list data based on cFlag
void processParagraphs(){
    struct node *temp = start;
    struct node *prev = start;
    //Replace all 'c' with 's' if followed by the characters 'e', 'i' or 'y i.e cFlag=1;
    if(cFlag == 1){
        while(temp!=NULL){
            if(temp->data=='c'){
                temp->data = 's';
            }
            temp = temp->next;
        }
    }
    else{ //otherwise Replace "sh" with ph
        while(temp!=NULL){
            prev = temp;
            temp = temp->next;
            if(temp!=NULL){
                if(prev->data=='s'&&temp->data=='h'){
                    prev->data = 'p';
                }
            }
        }
    }
}

//writeOutput() to generate a output file 
void writeOutput(){
    //filePath where output file is to be stored
    char filePath[20];
    strcpy(filePath,"output.txt");

    //file is opened
    FILE *fileOut = fopen(filePath,"w");
    if(fileOut==NULL){
        printf("\nFile Not Opened Correctly.");
        return;
    }

    //if file is successfully opened
    struct node *temp = start;

    //inserts all data into output file
    while(temp!=NULL){
        fputc(temp->data,fileOut);
        temp = temp->next;
    }

    //closing opened file
    fclose(fileOut);
}

//main() , program execution starts from here
int main(){
    //input file path is set
    char filePath[20];
    strcpy(filePath,"input.txt");

    //input file is opened for reading
    FILE *fileIn = fopen(filePath,"r"); 

    if(fileIn==NULL){
        printf("\nFile Not Opened Correctly.");
        return 0;
    }

    //if file opened successfully

    //ch variable to store characters from file
    char ch;

    //loop iterates till end of file is not reached
    while ((ch = fgetc(fileIn)) != EOF)
    {       
        //insert() is called to insert data into linked list
            insert(ch);
    }

    //input file is closed
    fclose(fileIn);

    //processParagraphs() called to process the linked list based on cFlag
    processParagraphs();

    //writeOutput() called to write in a output file
    writeOutput();

    return 0;
}

Instructions:

1. Must have input.txt file within the same directory upon which the program is going to work.

2.output.txt file will be generated by the program after processing input.txt file.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
SOLUTION IN C## Write a program that reads every line in a text file, line by...
SOLUTION IN C## Write a program that reads every line in a text file, line by line. Obtain the name of the existing file and the name of the new (copy) of the file by prompting the user for both of these on the console. Make sure that the original file exists and can be read. If not, display a warning message and allow the user to either abort the program or enter a new file name. If the name...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints the number of integer values in the file. Your program's input will be a string with the name of the file. If the file does not exist, then the program must print: Error opening the file For example, given the following CSV file input1.csv: 1,10,20,30,40 The output of your program must be: 5 You can safely assume that the input file will be a...
Write a program in python that reads in the file quiztext (txt) and creates a list...
Write a program in python that reads in the file quiztext (txt) and creates a list of each unique word used in the file, then prints the list. Generate a report that lists each individual word followed by the number of times it appears in the text, for example: Frequency_list = [('was', 3), ('bird',5), ('it', 27)….] and so on. Note: Notice the structure: a list of tuples. If you're really feeling daring, Google how to sort this new list based...
Employment Law Instructions · You must clearly identify the question you are answering in each of...
Employment Law Instructions · You must clearly identify the question you are answering in each of your answers. · You will be able to answer the questions based on the course materials (the text book, the Fair Work Act 2009 and the readings provided on Moodle). Referencing Please write your answers with reference to the relevant case law and legislation. · When you refer to a provision in the legislation, you must cite the provision and the legislation (eg s...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...
Discuss how the respective organizations’ relations with stakeholders could have potentially been affected by the events...
Discuss how the respective organizations’ relations with stakeholders could have potentially been affected by the events that took place at Enron and how the situation could have been dealt with differently to prevent further damage? THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies,...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Discuss ethical issues that can be identified in this case and the mode of managing ethics...
Discuss ethical issues that can be identified in this case and the mode of managing ethics Enron finds itself in this case. How would you describe the ethical culture and levels of trust at Enron? Provide reasons for your assessment. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described itself as "a quarterly survey of business leaders from across the globe … surveying 11,500 businesses in 40 economies across the globe on an annual basis." 1 According to the 2011 IBR, the Asia Pacific region had a higher percentage (27 percent) of female chief executive officers (CEOs) than Europe and North America. Japan is the only Asia Pacific region exception. The report further...